xilinx_devcfg.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. /*
  2. * Xilinx Zynq Device Config driver
  3. *
  4. * Copyright (c) 2011 - 2013 Xilinx Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/cdev.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/string.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/types.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/regmap.h>
  31. #include <linux/mfd/syscon.h>
  32. #define DRIVER_NAME "xdevcfg"
  33. #define XDEVCFG_DEVICES 1
  34. /* An array, which is set to true when the device is registered. */
  35. static DEFINE_MUTEX(xdevcfg_mutex);
  36. /* Constant Definitions */
  37. #define XDCFG_CTRL_OFFSET 0x00 /* Control Register */
  38. #define XDCFG_LOCK_OFFSET 0x04 /* Lock Register */
  39. #define XDCFG_INT_STS_OFFSET 0x0C /* Interrupt Status Register */
  40. #define XDCFG_INT_MASK_OFFSET 0x10 /* Interrupt Mask Register */
  41. #define XDCFG_STATUS_OFFSET 0x14 /* Status Register */
  42. #define XDCFG_DMA_SRC_ADDR_OFFSET 0x18 /* DMA Source Address Register */
  43. #define XDCFG_DMA_DEST_ADDR_OFFSET 0x1C /* DMA Destination Address Reg */
  44. #define XDCFG_DMA_SRC_LEN_OFFSET 0x20 /* DMA Source Transfer Length */
  45. #define XDCFG_DMA_DEST_LEN_OFFSET 0x24 /* DMA Destination Transfer */
  46. #define XDCFG_UNLOCK_OFFSET 0x34 /* Unlock Register */
  47. #define XDCFG_MCTRL_OFFSET 0x80 /* Misc. Control Register */
  48. /* Control Register Bit definitions */
  49. #define XDCFG_CTRL_PCFG_PROG_B_MASK 0x40000000 /* Program signal to
  50. * Reset FPGA
  51. */
  52. #define XDCFG_CTRL_PCAP_PR_MASK 0x08000000 /* Enable PCAP for PR */
  53. #define XDCFG_CTRL_PCAP_MODE_MASK 0x04000000 /* Enable PCAP */
  54. #define XDCFG_CTRL_PCAP_RATE_EN_MASK 0x02000000 /* Enable PCAP Quad Rate */
  55. #define XDCFG_CTRL_PCFG_AES_EN_MASK 0x00000E00 /* AES Enable Mask */
  56. #define XDCFG_CTRL_SEU_EN_MASK 0x00000100 /* SEU Enable Mask */
  57. #define XDCFG_CTRL_SPNIDEN_MASK 0x00000040 /* Secure Non Invasive
  58. * Debug Enable
  59. */
  60. #define XDCFG_CTRL_SPIDEN_MASK 0x00000020 /* Secure Invasive
  61. * Debug Enable
  62. */
  63. #define XDCFG_CTRL_NIDEN_MASK 0x00000010 /* Non-Invasive Debug
  64. * Enable
  65. */
  66. #define XDCFG_CTRL_DBGEN_MASK 0x00000008 /* Invasive Debug
  67. * Enable
  68. */
  69. #define XDCFG_CTRL_DAP_EN_MASK 0x00000007 /* DAP Enable Mask */
  70. /* Lock register bit definitions */
  71. #define XDCFG_LOCK_AES_EN_MASK 0x00000008 /* Lock AES_EN update */
  72. #define XDCFG_LOCK_SEU_MASK 0x00000004 /* Lock SEU_En update */
  73. #define XDCFG_LOCK_DBG_MASK 0x00000001 /* This bit locks
  74. * security config
  75. * including: DAP_En,
  76. * DBGEN,NIDEN, SPNIEN
  77. */
  78. /* Miscellaneous Control Register bit definitions */
  79. #define XDCFG_MCTRL_PCAP_LPBK_MASK 0x00000010 /* Internal PCAP loopback */
  80. /* Status register bit definitions */
  81. #define XDCFG_STATUS_PCFG_INIT_MASK 0x00000010 /* FPGA init status */
  82. /* Interrupt Status/Mask Register Bit definitions */
  83. #define XDCFG_IXR_DMA_DONE_MASK 0x00002000 /* DMA Command Done */
  84. #define XDCFG_IXR_D_P_DONE_MASK 0x00001000 /* DMA and PCAP Cmd Done */
  85. #define XDCFG_IXR_PCFG_DONE_MASK 0x00000004 /* FPGA programmed */
  86. #define XDCFG_IXR_ERROR_FLAGS_MASK 0x00F0F860
  87. #define XDCFG_IXR_ALL_MASK 0xF8F7F87F
  88. /* Miscellaneous constant values */
  89. #define XDCFG_DMA_INVALID_ADDRESS 0xFFFFFFFF /* Invalid DMA address */
  90. static const char * const fclk_name[] = {
  91. "fclk0",
  92. "fclk1",
  93. "fclk2",
  94. "fclk3"
  95. };
  96. #define NUMFCLKS ARRAY_SIZE(fclk_name)
  97. /**
  98. * struct xdevcfg_drvdata - Device Configuration driver structure
  99. *
  100. * @dev: Pointer to the device structure
  101. * @cdev: Instance of the cdev structure
  102. * @devt: Pointer to the dev_t structure
  103. * @class: Pointer to device class
  104. * @fclk_class: Pointer to fclk device class
  105. * @dma_done: The dma_done status bit for the DMA command completion
  106. * @error_status: The error status captured during the DMA transfer
  107. * @irq: Interrupt number
  108. * @clk: Peripheral clock for devcfg
  109. * @fclk: Array holding references to the FPGA clocks
  110. * @fclk_exported: Flag inidcating whether an FPGA clock is exported
  111. * @is_open: The status bit to indicate whether the device is opened
  112. * @sem: Instance for the mutex
  113. * @lock: Instance of spinlock
  114. * @base_address: The virtual device base address of the device registers
  115. * @ep107: Flags is used to identify the platform
  116. * @endian_swap: Flags is used to identify the endianness format
  117. * @residue_buf: Array holding stragglers from last time (0 to 3 bytes)
  118. * @residue_len: stragglers length in bytes
  119. * @is_partial_bitstream: Status bit to indicate partial/full bitstream
  120. */
  121. struct xdevcfg_drvdata {
  122. struct device *dev;
  123. struct cdev cdev;
  124. dev_t devt;
  125. struct class *class;
  126. struct class *fclk_class;
  127. int irq;
  128. struct clk *clk;
  129. struct clk *fclk[NUMFCLKS];
  130. u8 fclk_exported[NUMFCLKS];
  131. bool dma_done;
  132. int error_status;
  133. bool is_open;
  134. struct mutex sem;
  135. spinlock_t lock;
  136. void __iomem *base_address;
  137. int ep107;
  138. bool is_partial_bitstream;
  139. bool endian_swap;
  140. char residue_buf[3];
  141. int residue_len;
  142. };
  143. /**
  144. * struct fclk_data - FPGA clock data
  145. * @clk: Pointer to clock
  146. * @enabled: Flag indicating enable status of the clock
  147. * @rate_rnd: Rate to be rounded for round rate operation
  148. */
  149. struct fclk_data {
  150. struct clk *clk;
  151. int enabled;
  152. unsigned long rate_rnd;
  153. };
  154. /* Register read/write access routines */
  155. #define xdevcfg_writereg(offset, val) __raw_writel(val, offset)
  156. #define xdevcfg_readreg(offset) __raw_readl(offset)
  157. #define SLCR_FPGA_RST_CTRL_OFFSET 0x240 /* FPGA Software Reset Control */
  158. #define SLCR_LVL_SHFTR_EN_OFFSET 0x900 /* Level Shifters Enable */
  159. static struct regmap *zynq_slcr_regmap;
  160. /**
  161. * zynq_slcr_write - Write to a register in SLCR block
  162. *
  163. * @val: Value to write to the register
  164. * @offset: Register offset in SLCR block
  165. *
  166. * Return: a negative value on error, 0 on success
  167. */
  168. static int zynq_slcr_write(u32 val, u32 offset)
  169. {
  170. return regmap_write(zynq_slcr_regmap, offset, val);
  171. }
  172. /**
  173. * zynq_slcr_init_preload_fpga - Disable communication from the PL to PS.
  174. */
  175. static void zynq_slcr_init_preload_fpga(void)
  176. {
  177. /* Assert FPGA top level output resets */
  178. zynq_slcr_write(0xF, SLCR_FPGA_RST_CTRL_OFFSET);
  179. /* Disable level shifters */
  180. zynq_slcr_write(0, SLCR_LVL_SHFTR_EN_OFFSET);
  181. /* Enable output level shifters */
  182. zynq_slcr_write(0xA, SLCR_LVL_SHFTR_EN_OFFSET);
  183. }
  184. /**
  185. * zynq_slcr_init_postload_fpga - Re-enable communication from the PL to PS.
  186. */
  187. static void zynq_slcr_init_postload_fpga(void)
  188. {
  189. /* Enable level shifters */
  190. zynq_slcr_write(0xf, SLCR_LVL_SHFTR_EN_OFFSET);
  191. /* Deassert AXI interface resets */
  192. zynq_slcr_write(0, SLCR_FPGA_RST_CTRL_OFFSET);
  193. }
  194. /**
  195. * xdevcfg_reset_pl - Reset the programmable logic.
  196. * @base_address: The base address of the device.
  197. *
  198. * Must be called with PCAP clock enabled
  199. */
  200. static void xdevcfg_reset_pl(void __iomem *base_address)
  201. {
  202. /*
  203. * Create a rising edge on PCFG_INIT. PCFG_INIT follows PCFG_PROG_B,
  204. * so we need to * poll it after setting PCFG_PROG_B to make sure that
  205. * the rising edge happens.
  206. */
  207. xdevcfg_writereg(base_address + XDCFG_CTRL_OFFSET,
  208. (xdevcfg_readreg(base_address + XDCFG_CTRL_OFFSET) &
  209. ~XDCFG_CTRL_PCFG_PROG_B_MASK));
  210. while (xdevcfg_readreg(base_address + XDCFG_STATUS_OFFSET) &
  211. XDCFG_STATUS_PCFG_INIT_MASK)
  212. ;
  213. usleep_range(5000, 5100);
  214. xdevcfg_writereg(base_address + XDCFG_CTRL_OFFSET,
  215. (xdevcfg_readreg(base_address + XDCFG_CTRL_OFFSET) |
  216. XDCFG_CTRL_PCFG_PROG_B_MASK));
  217. while (!(xdevcfg_readreg(base_address + XDCFG_STATUS_OFFSET) &
  218. XDCFG_STATUS_PCFG_INIT_MASK))
  219. ;
  220. }
  221. /**
  222. * xdevcfg_irq - The main interrupt handler.
  223. * @irq: The interrupt number.
  224. * @data: Pointer to the driver data structure.
  225. * returns: IRQ_HANDLED after the interrupt is handled.
  226. **/
  227. static irqreturn_t xdevcfg_irq(int irq, void *data)
  228. {
  229. u32 intr_status;
  230. struct xdevcfg_drvdata *drvdata = data;
  231. spin_lock(&drvdata->lock);
  232. intr_status = xdevcfg_readreg(drvdata->base_address +
  233. XDCFG_INT_STS_OFFSET);
  234. /* Clear the interrupts */
  235. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
  236. intr_status);
  237. if ((intr_status & XDCFG_IXR_D_P_DONE_MASK) ==
  238. XDCFG_IXR_D_P_DONE_MASK)
  239. drvdata->dma_done = 1;
  240. if ((intr_status & XDCFG_IXR_ERROR_FLAGS_MASK) ==
  241. XDCFG_IXR_ERROR_FLAGS_MASK)
  242. drvdata->error_status = 1;
  243. spin_unlock(&drvdata->lock);
  244. return IRQ_HANDLED;
  245. }
  246. /**
  247. * xdevcfg_write - The is the driver write function.
  248. *
  249. * @file: Pointer to the file structure.
  250. * @buf: Pointer to the bitstream location.
  251. * @count: The number of bytes to be written.
  252. * @ppos: Pointer to the offset value
  253. * returns: Success or error status.
  254. **/
  255. static ssize_t
  256. xdevcfg_write(struct file *file, const char __user *buf, size_t count,
  257. loff_t *ppos)
  258. {
  259. char *kbuf;
  260. int status;
  261. unsigned long timeout;
  262. u32 intr_reg, dma_len;
  263. dma_addr_t dma_addr;
  264. u32 transfer_length = 0;
  265. struct xdevcfg_drvdata *drvdata = file->private_data;
  266. size_t user_count = count;
  267. int i;
  268. status = clk_enable(drvdata->clk);
  269. if (status)
  270. return status;
  271. status = mutex_lock_interruptible(&drvdata->sem);
  272. if (status)
  273. goto err_clk;
  274. dma_len = count + drvdata->residue_len;
  275. kbuf = dma_alloc_coherent(drvdata->dev, dma_len, &dma_addr, GFP_KERNEL);
  276. if (!kbuf) {
  277. status = -ENOMEM;
  278. goto err_unlock;
  279. }
  280. /* Collect stragglers from last time (0 to 3 bytes) */
  281. memcpy(kbuf, drvdata->residue_buf, drvdata->residue_len);
  282. /* Fetch user data, appending to stragglers */
  283. if (copy_from_user(kbuf + drvdata->residue_len, buf, count)) {
  284. status = -EFAULT;
  285. goto error;
  286. }
  287. /* Include stragglers in total bytes to be handled */
  288. count += drvdata->residue_len;
  289. /* First block contains a header */
  290. if (*ppos == 0 && count > 4) {
  291. /* Look for sync word */
  292. for (i = 0; i < count - 4; i++) {
  293. if (memcmp(kbuf + i, "\x66\x55\x99\xAA", 4) == 0) {
  294. pr_debug("Found normal sync word\n");
  295. drvdata->endian_swap = 0;
  296. break;
  297. }
  298. if (memcmp(kbuf + i, "\xAA\x99\x55\x66", 4) == 0) {
  299. pr_debug("Found swapped sync word\n");
  300. drvdata->endian_swap = 1;
  301. break;
  302. }
  303. }
  304. /* Remove the header, aligning the data on word boundary */
  305. if (i != count - 4) {
  306. count -= i;
  307. memmove(kbuf, kbuf + i, count);
  308. }
  309. }
  310. /* Save stragglers for next time */
  311. drvdata->residue_len = count % 4;
  312. count -= drvdata->residue_len;
  313. memcpy(drvdata->residue_buf, kbuf + count, drvdata->residue_len);
  314. /* Fixup endianess of the data */
  315. if (drvdata->endian_swap) {
  316. for (i = 0; i < count; i += 4) {
  317. u32 *p = (u32 *)&kbuf[i];
  318. *p = swab32(*p);
  319. }
  320. }
  321. /* Enable DMA and error interrupts */
  322. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
  323. XDCFG_IXR_ALL_MASK);
  324. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
  325. (u32) (~(XDCFG_IXR_D_P_DONE_MASK |
  326. XDCFG_IXR_ERROR_FLAGS_MASK)));
  327. drvdata->dma_done = 0;
  328. drvdata->error_status = 0;
  329. /* Initiate DMA write command */
  330. if (count < 0x1000)
  331. xdevcfg_writereg(drvdata->base_address +
  332. XDCFG_DMA_SRC_ADDR_OFFSET, (u32)(dma_addr + 1));
  333. else
  334. xdevcfg_writereg(drvdata->base_address +
  335. XDCFG_DMA_SRC_ADDR_OFFSET, (u32) dma_addr);
  336. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_ADDR_OFFSET,
  337. (u32)XDCFG_DMA_INVALID_ADDRESS);
  338. /* Convert number of bytes to number of words. */
  339. if (count % 4)
  340. transfer_length = (count / 4 + 1);
  341. else
  342. transfer_length = count / 4;
  343. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_SRC_LEN_OFFSET,
  344. transfer_length);
  345. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_LEN_OFFSET, 0);
  346. timeout = jiffies + msecs_to_jiffies(1000);
  347. while (!READ_ONCE(drvdata->dma_done)) {
  348. if (time_after(jiffies, timeout)) {
  349. status = -ETIMEDOUT;
  350. goto error;
  351. }
  352. }
  353. if (READ_ONCE(drvdata->error_status))
  354. status = drvdata->error_status;
  355. /* Disable the DMA and error interrupts */
  356. intr_reg = xdevcfg_readreg(drvdata->base_address +
  357. XDCFG_INT_MASK_OFFSET);
  358. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
  359. intr_reg | (XDCFG_IXR_D_P_DONE_MASK |
  360. XDCFG_IXR_ERROR_FLAGS_MASK));
  361. /* If we didn't write correctly, then bail out. */
  362. if (status) {
  363. status = -EFAULT;
  364. goto error;
  365. }
  366. *ppos += user_count;
  367. status = user_count;
  368. error:
  369. dma_free_coherent(drvdata->dev, dma_len, kbuf, dma_addr);
  370. err_unlock:
  371. mutex_unlock(&drvdata->sem);
  372. err_clk:
  373. clk_disable(drvdata->clk);
  374. return status;
  375. }
  376. /**
  377. * xdevcfg_read - The is the driver read function.
  378. * @file: Pointer to the file structure.
  379. * @buf: Pointer to the bitstream location.
  380. * @count: The number of bytes read.
  381. * @ppos: Pointer to the offsetvalue
  382. * returns: Success or error status.
  383. */
  384. static ssize_t
  385. xdevcfg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  386. {
  387. u32 *kbuf;
  388. int status;
  389. unsigned long timeout;
  390. dma_addr_t dma_addr;
  391. struct xdevcfg_drvdata *drvdata = file->private_data;
  392. u32 intr_reg;
  393. status = clk_enable(drvdata->clk);
  394. if (status)
  395. return status;
  396. status = mutex_lock_interruptible(&drvdata->sem);
  397. if (status)
  398. goto err_clk;
  399. /* Get new data from the ICAP, and return was requested. */
  400. kbuf = dma_alloc_coherent(drvdata->dev, count, &dma_addr, GFP_KERNEL);
  401. if (!kbuf) {
  402. status = -ENOMEM;
  403. goto err_unlock;
  404. }
  405. drvdata->dma_done = 0;
  406. drvdata->error_status = 0;
  407. /* Enable DMA and error interrupts */
  408. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
  409. XDCFG_IXR_ALL_MASK);
  410. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
  411. (u32) (~(XDCFG_IXR_D_P_DONE_MASK |
  412. XDCFG_IXR_ERROR_FLAGS_MASK)));
  413. /* Initiate DMA read command */
  414. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_SRC_ADDR_OFFSET,
  415. (u32)XDCFG_DMA_INVALID_ADDRESS);
  416. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_ADDR_OFFSET,
  417. (u32)dma_addr);
  418. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_SRC_LEN_OFFSET, 0);
  419. xdevcfg_writereg(drvdata->base_address + XDCFG_DMA_DEST_LEN_OFFSET,
  420. count / 4);
  421. timeout = jiffies + msecs_to_jiffies(1000);
  422. while (!drvdata->dma_done) {
  423. if (time_after(jiffies, timeout)) {
  424. status = -ETIMEDOUT;
  425. goto error;
  426. }
  427. }
  428. if (drvdata->error_status)
  429. status = drvdata->error_status;
  430. /* Disable and clear DMA and error interrupts */
  431. intr_reg = xdevcfg_readreg(drvdata->base_address +
  432. XDCFG_INT_MASK_OFFSET);
  433. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_MASK_OFFSET,
  434. intr_reg | (XDCFG_IXR_D_P_DONE_MASK |
  435. XDCFG_IXR_ERROR_FLAGS_MASK));
  436. /* If we didn't read correctly, then bail out. */
  437. if (status) {
  438. status = -EFAULT;
  439. goto error;
  440. }
  441. /* If we fail to return the data to the user, then bail out. */
  442. if (copy_to_user(buf, kbuf, count)) {
  443. status = -EFAULT;
  444. goto error;
  445. }
  446. status = count;
  447. error:
  448. dma_free_coherent(drvdata->dev, count, kbuf, dma_addr);
  449. err_unlock:
  450. mutex_unlock(&drvdata->sem);
  451. err_clk:
  452. clk_disable(drvdata->clk);
  453. return status;
  454. }
  455. static void xdevcfg_enable_partial(struct xdevcfg_drvdata *drvdata)
  456. {
  457. u32 reg = xdevcfg_readreg(drvdata->base_address + XDCFG_CTRL_OFFSET);
  458. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  459. reg | XDCFG_CTRL_PCAP_PR_MASK);
  460. }
  461. static void xdevcfg_disable_partial(struct xdevcfg_drvdata *drvdata)
  462. {
  463. u32 reg = xdevcfg_readreg(drvdata->base_address + XDCFG_CTRL_OFFSET);
  464. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  465. reg & ~XDCFG_CTRL_PCAP_PR_MASK);
  466. }
  467. /**
  468. * xdevcfg_open - The is the driver open function.
  469. * @inode: Pointer to the inode structure of this device.
  470. * @file: Pointer to the file structure.
  471. * returns: Success or error status.
  472. */
  473. static int xdevcfg_open(struct inode *inode, struct file *file)
  474. {
  475. struct xdevcfg_drvdata *drvdata;
  476. int status;
  477. drvdata = container_of(inode->i_cdev, struct xdevcfg_drvdata, cdev);
  478. status = clk_enable(drvdata->clk);
  479. if (status)
  480. return status;
  481. status = mutex_lock_interruptible(&drvdata->sem);
  482. if (status)
  483. goto err_clk;
  484. if (drvdata->is_open) {
  485. status = -EBUSY;
  486. goto error;
  487. }
  488. file->private_data = drvdata;
  489. drvdata->is_open = 1;
  490. drvdata->endian_swap = 0;
  491. drvdata->residue_len = 0;
  492. /*
  493. * If is_partial_bitstream is set, then PROG_B is not asserted
  494. * (xdevcfg_reset_pl function) and also zynq_slcr_init_preload_fpga and
  495. * zynq_slcr_init_postload_fpga functions are not invoked.
  496. */
  497. if (drvdata->is_partial_bitstream)
  498. xdevcfg_enable_partial(drvdata);
  499. else
  500. zynq_slcr_init_preload_fpga();
  501. /*
  502. * Only do the reset of the PL for Zynq as it causes problems on the
  503. * EP107 and the issue is not understood, but not worth investigating
  504. * as the emulation platform is very different than silicon and not a
  505. * complete implementation. Also, do not reset if it is a partial
  506. * bitstream.
  507. */
  508. if ((!drvdata->ep107) && (!drvdata->is_partial_bitstream))
  509. xdevcfg_reset_pl(drvdata->base_address);
  510. xdevcfg_writereg(drvdata->base_address + XDCFG_INT_STS_OFFSET,
  511. XDCFG_IXR_PCFG_DONE_MASK);
  512. error:
  513. mutex_unlock(&drvdata->sem);
  514. err_clk:
  515. clk_disable(drvdata->clk);
  516. return status;
  517. }
  518. /**
  519. * xdevcfg_release - The is the driver release function.
  520. * @inode: Pointer to the inode structure of this device.
  521. * @file: Pointer to the file structure.
  522. * returns: Success.
  523. */
  524. static int xdevcfg_release(struct inode *inode, struct file *file)
  525. {
  526. struct xdevcfg_drvdata *drvdata = file->private_data;
  527. if (drvdata->is_partial_bitstream)
  528. xdevcfg_disable_partial(drvdata);
  529. else
  530. zynq_slcr_init_postload_fpga();
  531. if (drvdata->residue_len)
  532. dev_info(drvdata->dev, "Did not transfer last %d bytes\n",
  533. drvdata->residue_len);
  534. drvdata->is_open = 0;
  535. return 0;
  536. }
  537. static const struct file_operations xdevcfg_fops = {
  538. .owner = THIS_MODULE,
  539. .write = xdevcfg_write,
  540. .read = xdevcfg_read,
  541. .open = xdevcfg_open,
  542. .release = xdevcfg_release,
  543. };
  544. /*
  545. * The following functions are the routines provided to the user to
  546. * set/get the status bit value in the control/lock registers.
  547. */
  548. /**
  549. * xdevcfg_set_dap_en - This function sets the DAP bits in the
  550. * control register with the given value.
  551. * @dev: Pointer to the device structure.
  552. * @attr: Pointer to the device attribute structure.
  553. * @buf: Pointer to the buffer location for the configuration
  554. * data.
  555. * @size: The number of bytes used from the buffer
  556. * returns: negative error if the string could not be converted
  557. * or the size of the buffer.
  558. */
  559. static ssize_t xdevcfg_set_dap_en(struct device *dev,
  560. struct device_attribute *attr, const char *buf, size_t size)
  561. {
  562. u32 ctrl_reg_status;
  563. unsigned long flags;
  564. unsigned long mask_bit;
  565. int status;
  566. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  567. status = clk_enable(drvdata->clk);
  568. if (status)
  569. return status;
  570. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  571. XDCFG_CTRL_OFFSET);
  572. spin_lock_irqsave(&drvdata->lock, flags);
  573. status = kstrtoul(buf, 10, &mask_bit);
  574. if (status)
  575. goto err_unlock;
  576. if (mask_bit > 7) {
  577. status = -EINVAL;
  578. goto err_unlock;
  579. }
  580. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  581. (ctrl_reg_status |
  582. (((u32)mask_bit) & XDCFG_CTRL_DAP_EN_MASK)));
  583. spin_unlock_irqrestore(&drvdata->lock, flags);
  584. clk_disable(drvdata->clk);
  585. return size;
  586. err_unlock:
  587. spin_unlock_irqrestore(&drvdata->lock, flags);
  588. clk_disable(drvdata->clk);
  589. return status;
  590. }
  591. /**
  592. * xdevcfg_show_dap_en_status - The function returns the DAP_EN bits status in
  593. * the control register.
  594. * @dev: Pointer to the device structure.
  595. * @attr: Pointer to the device attribute structure.
  596. * @buf: Pointer to the buffer location for the configuration
  597. * data.
  598. * returns: Size of the buffer.
  599. */
  600. static ssize_t xdevcfg_show_dap_en_status(struct device *dev,
  601. struct device_attribute *attr, char *buf)
  602. {
  603. u32 dap_en_status;
  604. int status;
  605. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  606. status = clk_enable(drvdata->clk);
  607. if (status)
  608. return status;
  609. dap_en_status = xdevcfg_readreg(drvdata->base_address +
  610. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_DAP_EN_MASK;
  611. clk_disable(drvdata->clk);
  612. status = sprintf(buf, "%d\n", dap_en_status);
  613. return status;
  614. }
  615. static DEVICE_ATTR(enable_dap, 0644, xdevcfg_show_dap_en_status,
  616. xdevcfg_set_dap_en);
  617. /**
  618. * xdevcfg_set_dbgen - This function sets the DBGEN bit in the
  619. * control register with the given value.
  620. * @dev: Pointer to the device structure.
  621. * @attr: Pointer to the device attribute structure.
  622. * @buf: Pointer to the buffer location for the configuration
  623. * data.
  624. * @size: The number of bytes used from the buffer
  625. * returns: -EINVAL if invalid parameter is sent or size
  626. */
  627. static ssize_t xdevcfg_set_dbgen(struct device *dev,
  628. struct device_attribute *attr, const char *buf, size_t size)
  629. {
  630. u32 ctrl_reg_status;
  631. unsigned long flags;
  632. unsigned long mask_bit;
  633. int status;
  634. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  635. status = clk_enable(drvdata->clk);
  636. if (status)
  637. return status;
  638. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  639. XDCFG_CTRL_OFFSET);
  640. status = kstrtoul(buf, 10, &mask_bit);
  641. if (status)
  642. goto err_clk;
  643. if (mask_bit > 1) {
  644. status = -EINVAL;
  645. goto err_clk;
  646. }
  647. spin_lock_irqsave(&drvdata->lock, flags);
  648. if (mask_bit)
  649. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  650. (ctrl_reg_status | XDCFG_CTRL_DBGEN_MASK));
  651. else
  652. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  653. (ctrl_reg_status & (~XDCFG_CTRL_DBGEN_MASK)));
  654. spin_unlock_irqrestore(&drvdata->lock, flags);
  655. clk_disable(drvdata->clk);
  656. return size;
  657. err_clk:
  658. clk_disable(drvdata->clk);
  659. return status;
  660. }
  661. /**
  662. * xdevcfg_show_dbgen_status - The function returns the DBGEN bit status in
  663. * the control register.
  664. * @dev: Pointer to the device structure.
  665. * @attr: Pointer to the device attribute structure.
  666. * @buf: Pointer to the buffer location for the configuration
  667. * data.
  668. * returns: Size of the buffer.
  669. */
  670. static ssize_t xdevcfg_show_dbgen_status(struct device *dev,
  671. struct device_attribute *attr, char *buf)
  672. {
  673. u32 dbgen_status;
  674. ssize_t status;
  675. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  676. status = clk_enable(drvdata->clk);
  677. if (status)
  678. return status;
  679. dbgen_status = xdevcfg_readreg(drvdata->base_address +
  680. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_DBGEN_MASK;
  681. clk_disable(drvdata->clk);
  682. status = sprintf(buf, "%d\n", (dbgen_status >> 3));
  683. return status;
  684. }
  685. static DEVICE_ATTR(enable_dbg_in, 0644, xdevcfg_show_dbgen_status,
  686. xdevcfg_set_dbgen);
  687. /**
  688. * xdevcfg_set_niden - This function sets the NIDEN bit in the
  689. * control register with the given value.
  690. * @dev: Pointer to the device structure.
  691. * @attr: Pointer to the device attribute structure.
  692. * @buf: Pointer to the buffer location for the configuration
  693. * data.
  694. * @size: The number of bytes used from the buffer
  695. * returns: -EINVAL if invalid parameter is sent or size
  696. */
  697. static ssize_t xdevcfg_set_niden(struct device *dev,
  698. struct device_attribute *attr, const char *buf, size_t size)
  699. {
  700. u32 ctrl_reg_status;
  701. unsigned long flags;
  702. unsigned long mask_bit;
  703. int status;
  704. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  705. status = clk_enable(drvdata->clk);
  706. if (status)
  707. return status;
  708. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  709. XDCFG_CTRL_OFFSET);
  710. status = kstrtoul(buf, 10, &mask_bit);
  711. if (status)
  712. goto err_clk;
  713. if (mask_bit > 1) {
  714. status = -EINVAL;
  715. goto err_clk;
  716. }
  717. spin_lock_irqsave(&drvdata->lock, flags);
  718. if (mask_bit)
  719. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  720. (ctrl_reg_status | XDCFG_CTRL_NIDEN_MASK));
  721. else
  722. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  723. (ctrl_reg_status & (~XDCFG_CTRL_NIDEN_MASK)));
  724. spin_unlock_irqrestore(&drvdata->lock, flags);
  725. clk_disable(drvdata->clk);
  726. return size;
  727. err_clk:
  728. clk_disable(drvdata->clk);
  729. return status;
  730. }
  731. /**
  732. * xdevcfg_show_niden_status - The function returns the NIDEN bit status in
  733. * the control register.
  734. * @dev: Pointer to the device structure.
  735. * @attr: Pointer to the device attribute structure.
  736. * @buf: Pointer to the buffer location for the configuration
  737. * data.
  738. * returns: Size of the buffer.
  739. */
  740. static ssize_t xdevcfg_show_niden_status(struct device *dev,
  741. struct device_attribute *attr, char *buf)
  742. {
  743. u32 niden_status;
  744. ssize_t status;
  745. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  746. status = clk_enable(drvdata->clk);
  747. if (status)
  748. return status;
  749. niden_status = xdevcfg_readreg(drvdata->base_address +
  750. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_NIDEN_MASK;
  751. clk_disable(drvdata->clk);
  752. status = sprintf(buf, "%d\n", (niden_status >> 4));
  753. return status;
  754. }
  755. static DEVICE_ATTR(enable_dbg_nonin, 0644, xdevcfg_show_niden_status,
  756. xdevcfg_set_niden);
  757. /**
  758. * xdevcfg_set_spiden - This function sets the SPIDEN bit in the
  759. * control register with the given value.
  760. * @dev: Pointer to the device structure.
  761. * @attr: Pointer to the device attribute structure.
  762. * @buf: Pointer to the buffer location for the configuration
  763. * data.
  764. * @size: The number of bytes used from the buffer
  765. * returns: -EINVAL if invalid parameter is sent or size
  766. */
  767. static ssize_t xdevcfg_set_spiden(struct device *dev,
  768. struct device_attribute *attr, const char *buf, size_t size)
  769. {
  770. u32 ctrl_reg_status;
  771. unsigned long flags;
  772. unsigned long mask_bit;
  773. int status;
  774. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  775. status = clk_enable(drvdata->clk);
  776. if (status)
  777. return status;
  778. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  779. XDCFG_CTRL_OFFSET);
  780. status = kstrtoul(buf, 10, &mask_bit);
  781. if (status)
  782. goto err_clk;
  783. if (mask_bit > 1) {
  784. status = -EINVAL;
  785. goto err_clk;
  786. }
  787. spin_lock_irqsave(&drvdata->lock, flags);
  788. if (mask_bit)
  789. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  790. (ctrl_reg_status | XDCFG_CTRL_SPIDEN_MASK));
  791. else
  792. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  793. (ctrl_reg_status & (~XDCFG_CTRL_SPIDEN_MASK)));
  794. spin_unlock_irqrestore(&drvdata->lock, flags);
  795. clk_disable(drvdata->clk);
  796. return size;
  797. err_clk:
  798. clk_disable(drvdata->clk);
  799. return status;
  800. }
  801. /**
  802. * xdevcfg_show_spiden_status - The function returns the SPIDEN bit status in
  803. * the control register.
  804. * @dev: Pointer to the device structure.
  805. * @attr: Pointer to the device attribute structure.
  806. * @buf: Pointer to the buffer location for the configuration
  807. * data.
  808. * returns: Size of the buffer.
  809. */
  810. static ssize_t xdevcfg_show_spiden_status(struct device *dev,
  811. struct device_attribute *attr, char *buf)
  812. {
  813. u32 spiden_status;
  814. ssize_t status;
  815. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  816. status = clk_enable(drvdata->clk);
  817. if (status)
  818. return status;
  819. spiden_status = xdevcfg_readreg(drvdata->base_address +
  820. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_SPIDEN_MASK;
  821. clk_disable(drvdata->clk);
  822. status = sprintf(buf, "%d\n", (spiden_status >> 5));
  823. return status;
  824. }
  825. static DEVICE_ATTR(enable_sec_dbg_in, 0644, xdevcfg_show_spiden_status,
  826. xdevcfg_set_spiden);
  827. /**
  828. * xdevcfg_set_spniden - This function sets the SPNIDEN bit in the
  829. * control register with the given value.
  830. * @dev: Pointer to the device structure.
  831. * @attr: Pointer to the device attribute structure.
  832. * @buf: Pointer to the buffer location for the configuration
  833. * data.
  834. * @size: The number of bytes used from the buffer
  835. * returns: -EINVAL if invalid parameter is sent or the size of buffer
  836. */
  837. static ssize_t xdevcfg_set_spniden(struct device *dev,
  838. struct device_attribute *attr, const char *buf, size_t size)
  839. {
  840. u32 ctrl_reg_status;
  841. unsigned long flags;
  842. unsigned long mask_bit;
  843. ssize_t status;
  844. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  845. status = clk_enable(drvdata->clk);
  846. if (status)
  847. return status;
  848. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  849. XDCFG_CTRL_OFFSET);
  850. status = kstrtoul(buf, 10, &mask_bit);
  851. if (status)
  852. goto err_clk;
  853. if (mask_bit > 1) {
  854. status = -EINVAL;
  855. goto err_clk;
  856. }
  857. spin_lock_irqsave(&drvdata->lock, flags);
  858. if (mask_bit)
  859. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  860. (ctrl_reg_status | XDCFG_CTRL_SPNIDEN_MASK));
  861. else
  862. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  863. (ctrl_reg_status & (~XDCFG_CTRL_SPNIDEN_MASK)));
  864. spin_unlock_irqrestore(&drvdata->lock, flags);
  865. clk_disable(drvdata->clk);
  866. return size;
  867. err_clk:
  868. clk_disable(drvdata->clk);
  869. return status;
  870. }
  871. /**
  872. * xdevcfg_show_spniden_status - The function returns the SPNIDEN bit status
  873. * in the control register.
  874. * @dev: Pointer to the device structure.
  875. * @attr: Pointer to the device attribute structure.
  876. * @buf: Pointer to the buffer location for the configuration
  877. * data.
  878. * returns: Size of the buffer.
  879. */
  880. static ssize_t xdevcfg_show_spniden_status(struct device *dev,
  881. struct device_attribute *attr, char *buf)
  882. {
  883. u32 spniden_status;
  884. ssize_t status;
  885. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  886. status = clk_enable(drvdata->clk);
  887. if (status)
  888. return status;
  889. spniden_status = xdevcfg_readreg(drvdata->base_address +
  890. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_SPNIDEN_MASK;
  891. clk_disable(drvdata->clk);
  892. status = sprintf(buf, "%d\n", (spniden_status >> 6));
  893. return status;
  894. }
  895. static DEVICE_ATTR(enable_sec_dbg_nonin, 0644, xdevcfg_show_spniden_status,
  896. xdevcfg_set_spniden);
  897. /**
  898. * xdevcfg_set_seu - This function sets the SEU_EN bit in the
  899. * control register with the given value
  900. * @dev: Pointer to the device structure.
  901. * @attr: Pointer to the device attribute structure.
  902. * @buf: Pointer to the buffer location for the configuration
  903. * data.
  904. * @size: The number of bytes used from the buffer
  905. * returns: -EINVAL if invalid parameter is sent or size
  906. */
  907. static ssize_t xdevcfg_set_seu(struct device *dev,
  908. struct device_attribute *attr, const char *buf, size_t size)
  909. {
  910. u32 ctrl_reg_status;
  911. unsigned long flags;
  912. unsigned long mask_bit;
  913. ssize_t status;
  914. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  915. status = clk_enable(drvdata->clk);
  916. if (status)
  917. return status;
  918. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  919. XDCFG_CTRL_OFFSET);
  920. status = kstrtoul(buf, 10, &mask_bit);
  921. if (status)
  922. goto err_clk;
  923. if (mask_bit > 1) {
  924. status = -EINVAL;
  925. goto err_clk;
  926. }
  927. spin_lock_irqsave(&drvdata->lock, flags);
  928. if (mask_bit)
  929. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  930. (ctrl_reg_status | XDCFG_CTRL_SEU_EN_MASK));
  931. else
  932. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  933. (ctrl_reg_status & (~XDCFG_CTRL_SEU_EN_MASK)));
  934. spin_unlock_irqrestore(&drvdata->lock, flags);
  935. clk_disable(drvdata->clk);
  936. return size;
  937. err_clk:
  938. clk_disable(drvdata->clk);
  939. return status;
  940. }
  941. /**
  942. * xdevcfg_show_seu_status - The function returns the SEU_EN bit status
  943. * in the control register.
  944. * @dev: Pointer to the device structure.
  945. * @attr: Pointer to the device attribute structure.
  946. * @buf: Pointer to the buffer location for the configuration
  947. * data.
  948. * returns: size of the buffer.
  949. */
  950. static ssize_t xdevcfg_show_seu_status(struct device *dev,
  951. struct device_attribute *attr, char *buf)
  952. {
  953. u32 seu_status;
  954. ssize_t status;
  955. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  956. status = clk_enable(drvdata->clk);
  957. if (status)
  958. return status;
  959. seu_status = xdevcfg_readreg(drvdata->base_address +
  960. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_SEU_EN_MASK;
  961. clk_disable(drvdata->clk);
  962. status = sprintf(buf, "%d\n", (seu_status > 8));
  963. return status;
  964. }
  965. static DEVICE_ATTR(enable_seu, 0644, xdevcfg_show_seu_status, xdevcfg_set_seu);
  966. /**
  967. * xdevcfg_set_aes - This function sets the AES_EN bits in the
  968. * control register with either all 1s or all 0s.
  969. * @dev: Pointer to the device structure.
  970. * @attr: Pointer to the device attribute structure.
  971. * @buf: Pointer to the buffer location for the configuration
  972. * data.
  973. * @size: The number of bytes used from the buffer
  974. * returns: -EINVAL if invalid parameter is sent or size
  975. *
  976. * The user must send only one bit in the buffer to notify whether he wants to
  977. * either set or reset these bits.
  978. */
  979. static ssize_t xdevcfg_set_aes(struct device *dev,
  980. struct device_attribute *attr, const char *buf, size_t size)
  981. {
  982. u32 ctrl_reg_status;
  983. unsigned long flags;
  984. unsigned long mask_bit;
  985. int status;
  986. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  987. status = clk_enable(drvdata->clk);
  988. if (status)
  989. return status;
  990. ctrl_reg_status = xdevcfg_readreg(drvdata->base_address +
  991. XDCFG_CTRL_OFFSET);
  992. status = kstrtoul(buf, 10, &mask_bit);
  993. if (status < 0)
  994. goto err_clk;
  995. if (mask_bit > 1) {
  996. status = -EINVAL;
  997. goto err_clk;
  998. }
  999. spin_lock_irqsave(&drvdata->lock, flags);
  1000. if (mask_bit)
  1001. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  1002. (ctrl_reg_status |
  1003. XDCFG_CTRL_PCFG_AES_EN_MASK |
  1004. XDCFG_CTRL_PCAP_RATE_EN_MASK));
  1005. else
  1006. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET,
  1007. (ctrl_reg_status &
  1008. ~(XDCFG_CTRL_PCFG_AES_EN_MASK |
  1009. XDCFG_CTRL_PCAP_RATE_EN_MASK)));
  1010. spin_unlock_irqrestore(&drvdata->lock, flags);
  1011. clk_disable(drvdata->clk);
  1012. return size;
  1013. err_clk:
  1014. clk_disable(drvdata->clk);
  1015. return status;
  1016. }
  1017. /**
  1018. * xdevcfg_show_aes_status - The function returns the AES_EN bit status
  1019. * in the control register.
  1020. * @dev: Pointer to the device structure.
  1021. * @attr: Pointer to the device attribute structure.
  1022. * @buf: Pointer to the buffer location for the configuration
  1023. * data.
  1024. * returns: size of the buffer.
  1025. */
  1026. static ssize_t xdevcfg_show_aes_status(struct device *dev,
  1027. struct device_attribute *attr, char *buf)
  1028. {
  1029. u32 aes_status;
  1030. ssize_t status;
  1031. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1032. status = clk_enable(drvdata->clk);
  1033. if (status)
  1034. return status;
  1035. aes_status = xdevcfg_readreg(drvdata->base_address +
  1036. XDCFG_CTRL_OFFSET) & XDCFG_CTRL_PCFG_AES_EN_MASK;
  1037. clk_disable(drvdata->clk);
  1038. status = sprintf(buf, "%d\n", (aes_status >> 9));
  1039. return status;
  1040. }
  1041. static DEVICE_ATTR(enable_aes, 0644, xdevcfg_show_aes_status, xdevcfg_set_aes);
  1042. /**
  1043. * xdevcfg_set_aes_en_lock - This function sets the LOCK_AES_EN bit in the
  1044. * lock register.
  1045. * @dev: Pointer to the device structure.
  1046. * @attr: Pointer to the device attribute structure.
  1047. * @buf: Pointer to the buffer location for the configuration
  1048. * data.
  1049. * @size: The number of bytes used from the buffer
  1050. * returns: -EINVAL if invalid parameter is sent or size
  1051. */
  1052. static ssize_t xdevcfg_set_aes_en_lock(struct device *dev,
  1053. struct device_attribute *attr, const char *buf, size_t size)
  1054. {
  1055. u32 aes_en_lock_status;
  1056. unsigned long flags;
  1057. unsigned long mask_bit;
  1058. ssize_t status;
  1059. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1060. status = clk_enable(drvdata->clk);
  1061. if (status)
  1062. return status;
  1063. aes_en_lock_status = xdevcfg_readreg(drvdata->base_address +
  1064. XDCFG_LOCK_OFFSET);
  1065. status = kstrtoul(buf, 10, &mask_bit);
  1066. if (status)
  1067. goto err_clk;
  1068. if (mask_bit > 1) {
  1069. status = -EINVAL;
  1070. goto err_clk;
  1071. }
  1072. spin_lock_irqsave(&drvdata->lock, flags);
  1073. if (mask_bit)
  1074. xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
  1075. (aes_en_lock_status | XDCFG_LOCK_AES_EN_MASK));
  1076. else
  1077. xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
  1078. (aes_en_lock_status &
  1079. (~XDCFG_LOCK_AES_EN_MASK)));
  1080. spin_unlock_irqrestore(&drvdata->lock, flags);
  1081. clk_disable(drvdata->clk);
  1082. return size;
  1083. err_clk:
  1084. clk_disable(drvdata->clk);
  1085. return status;
  1086. }
  1087. /**
  1088. * xdevcfg_show_aes_en_lock_status - The function returns the LOCK_AES_EN bit
  1089. * status in the lock register.
  1090. * @dev: Pointer to the device structure.
  1091. * @attr: Pointer to the device attribute structure.
  1092. * @buf: Pointer to the buffer location for the configuration
  1093. * data.
  1094. * returns: size of the buffer.
  1095. */
  1096. static ssize_t xdevcfg_show_aes_en_lock_status(struct device *dev,
  1097. struct device_attribute *attr, char *buf)
  1098. {
  1099. u32 aes_en_lock_status;
  1100. ssize_t status;
  1101. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1102. status = clk_enable(drvdata->clk);
  1103. if (status)
  1104. return status;
  1105. aes_en_lock_status = xdevcfg_readreg(drvdata->base_address +
  1106. XDCFG_LOCK_OFFSET) & XDCFG_LOCK_AES_EN_MASK;
  1107. clk_disable(drvdata->clk);
  1108. status = sprintf(buf, "%d\n", (aes_en_lock_status >> 3));
  1109. return status;
  1110. }
  1111. static DEVICE_ATTR(aes_en_lock, 0644, xdevcfg_show_aes_en_lock_status,
  1112. xdevcfg_set_aes_en_lock);
  1113. /**
  1114. * xdevcfg_set_seu_lock - This function sets the LOCK_SEU bit in the
  1115. * lock register.
  1116. * @dev: Pointer to the device structure.
  1117. * @attr: Pointer to the device attribute structure.
  1118. * @buf: Pointer to the buffer location for the configuration
  1119. * data.
  1120. * @size: The number of bytes used from the buffer
  1121. * returns: -EINVAL if invalid parameter is sent or size
  1122. */
  1123. static ssize_t xdevcfg_set_seu_lock(struct device *dev,
  1124. struct device_attribute *attr, const char *buf, size_t size)
  1125. {
  1126. u32 seu_lock_status;
  1127. unsigned long flags;
  1128. unsigned long mask_bit;
  1129. ssize_t status;
  1130. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1131. status = clk_enable(drvdata->clk);
  1132. if (status)
  1133. return status;
  1134. seu_lock_status = xdevcfg_readreg(drvdata->base_address +
  1135. XDCFG_LOCK_OFFSET);
  1136. status = kstrtoul(buf, 10, &mask_bit);
  1137. if (status)
  1138. goto err_clk;
  1139. if (mask_bit > 1) {
  1140. status = -EINVAL;
  1141. goto err_clk;
  1142. }
  1143. spin_lock_irqsave(&drvdata->lock, flags);
  1144. if (mask_bit)
  1145. xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
  1146. (seu_lock_status | XDCFG_LOCK_SEU_MASK));
  1147. else
  1148. xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
  1149. (seu_lock_status & (~XDCFG_LOCK_SEU_MASK)));
  1150. spin_unlock_irqrestore(&drvdata->lock, flags);
  1151. clk_disable(drvdata->clk);
  1152. return size;
  1153. err_clk:
  1154. clk_disable(drvdata->clk);
  1155. return status;
  1156. }
  1157. /**
  1158. * xdevcfg_show_seu_lock_status - The function returns the LOCK_SEU bit
  1159. * status in the lock register.
  1160. * @dev: Pointer to the device structure.
  1161. * @attr: Pointer to the device attribute structure.
  1162. * @buf: Pointer to the buffer location for the configuration
  1163. * data.
  1164. * returns: size of the buffer.
  1165. */
  1166. static ssize_t xdevcfg_show_seu_lock_status(struct device *dev,
  1167. struct device_attribute *attr, char *buf)
  1168. {
  1169. u32 seu_lock_status;
  1170. ssize_t status;
  1171. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1172. status = clk_enable(drvdata->clk);
  1173. if (status)
  1174. return status;
  1175. seu_lock_status = xdevcfg_readreg(drvdata->base_address +
  1176. XDCFG_LOCK_OFFSET) & XDCFG_LOCK_SEU_MASK;
  1177. clk_disable(drvdata->clk);
  1178. status = sprintf(buf, "%d\n", (seu_lock_status >> 2));
  1179. return status;
  1180. }
  1181. static DEVICE_ATTR(seu_lock, 0644, xdevcfg_show_seu_lock_status,
  1182. xdevcfg_set_seu_lock);
  1183. /**
  1184. * xdevcfg_set_dbg_lock - This function sets the LOCK_DBG bit in the
  1185. * lock register.
  1186. * @dev: Pointer to the device structure.
  1187. * @attr: Pointer to the device attribute structure.
  1188. * @buf: Pointer to the buffer location for the configuration
  1189. * data.
  1190. * @size: The number of bytes used from the buffer
  1191. * returns: -EINVAL if invalid parameter is sent or size
  1192. */
  1193. static ssize_t xdevcfg_set_dbg_lock(struct device *dev,
  1194. struct device_attribute *attr, const char *buf, size_t size)
  1195. {
  1196. u32 lock_reg_status;
  1197. unsigned long flags;
  1198. unsigned long mask_bit;
  1199. ssize_t status;
  1200. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1201. status = clk_enable(drvdata->clk);
  1202. if (status)
  1203. return status;
  1204. lock_reg_status = xdevcfg_readreg(drvdata->base_address +
  1205. XDCFG_LOCK_OFFSET);
  1206. status = kstrtoul(buf, 10, &mask_bit);
  1207. if (status)
  1208. goto err_clk;
  1209. if (mask_bit > 1) {
  1210. status = -EINVAL;
  1211. goto err_clk;
  1212. }
  1213. spin_lock_irqsave(&drvdata->lock, flags);
  1214. if (mask_bit)
  1215. xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
  1216. (lock_reg_status | XDCFG_LOCK_DBG_MASK));
  1217. else
  1218. xdevcfg_writereg(drvdata->base_address + XDCFG_LOCK_OFFSET,
  1219. (lock_reg_status & (~XDCFG_LOCK_DBG_MASK)));
  1220. spin_unlock_irqrestore(&drvdata->lock, flags);
  1221. clk_disable(drvdata->clk);
  1222. return size;
  1223. err_clk:
  1224. clk_disable(drvdata->clk);
  1225. return status;
  1226. }
  1227. /**
  1228. * xdevcfg_show_dbg_lock_status - The function returns the LOCK_DBG bit
  1229. * status in the lock register.
  1230. * @dev: Pointer to the device structure.
  1231. * @attr: Pointer to the device attribute structure.
  1232. * @buf: Pointer to the buffer location for the configuration
  1233. * data.
  1234. * returns: size of the buffer.
  1235. */
  1236. static ssize_t xdevcfg_show_dbg_lock_status(struct device *dev,
  1237. struct device_attribute *attr, char *buf)
  1238. {
  1239. u32 dbg_lock_status;
  1240. ssize_t status;
  1241. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1242. status = clk_enable(drvdata->clk);
  1243. if (status)
  1244. return status;
  1245. dbg_lock_status = xdevcfg_readreg(drvdata->base_address +
  1246. XDCFG_LOCK_OFFSET) & XDCFG_LOCK_DBG_MASK;
  1247. clk_disable(drvdata->clk);
  1248. status = sprintf(buf, "%d\n", dbg_lock_status);
  1249. return status;
  1250. }
  1251. static DEVICE_ATTR(dbg_lock, 0644, xdevcfg_show_dbg_lock_status,
  1252. xdevcfg_set_dbg_lock);
  1253. /**
  1254. * xdevcfg_show_prog_done_status - The function returns the PROG_DONE bit
  1255. * status in the interrupt status register.
  1256. * @dev: Pointer to the device structure.
  1257. * @attr: Pointer to the device attribute structure.
  1258. * @buf: Pointer to the buffer location for the configuration
  1259. * data.
  1260. * returns: size of the buffer.
  1261. */
  1262. static ssize_t xdevcfg_show_prog_done_status(struct device *dev,
  1263. struct device_attribute *attr, char *buf)
  1264. {
  1265. u32 prog_done_status;
  1266. ssize_t status;
  1267. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1268. status = clk_enable(drvdata->clk);
  1269. if (status)
  1270. return status;
  1271. prog_done_status = xdevcfg_readreg(drvdata->base_address +
  1272. XDCFG_INT_STS_OFFSET) & XDCFG_IXR_PCFG_DONE_MASK;
  1273. clk_disable(drvdata->clk);
  1274. status = sprintf(buf, "%d\n", (prog_done_status >> 2));
  1275. return status;
  1276. }
  1277. static DEVICE_ATTR(prog_done, 0644, xdevcfg_show_prog_done_status,
  1278. NULL);
  1279. /**
  1280. * xdevcfg_set_is_partial_bitstream - This function sets the
  1281. * is_partial_bitstream variable. If is_partial_bitstream is set,
  1282. * then PROG_B is not asserted (xdevcfg_reset_pl) and also
  1283. * zynq_slcr_init_preload_fpga and zynq_slcr_init_postload_fpga functions
  1284. * are not invoked.
  1285. * @dev: Pointer to the device structure.
  1286. * @attr: Pointer to the device attribute structure.
  1287. * @buf: Pointer to the buffer location for the configuration
  1288. * data.
  1289. * @size: The number of bytes used from the buffer
  1290. * returns: -EINVAL if invalid parameter is sent or size
  1291. */
  1292. static ssize_t xdevcfg_set_is_partial_bitstream(struct device *dev,
  1293. struct device_attribute *attr, const char *buf, size_t size)
  1294. {
  1295. unsigned long mask_bit;
  1296. ssize_t status;
  1297. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1298. status = kstrtoul(buf, 10, &mask_bit);
  1299. if (status)
  1300. return status;
  1301. if (mask_bit > 1)
  1302. return -EINVAL;
  1303. if (mask_bit)
  1304. drvdata->is_partial_bitstream = 1;
  1305. else
  1306. drvdata->is_partial_bitstream = 0;
  1307. return size;
  1308. }
  1309. /**
  1310. * xdevcfg_show_is_partial_bitstream_status - The function returns the
  1311. * value of is_partial_bitstream variable.
  1312. * @dev: Pointer to the device structure.
  1313. * @attr: Pointer to the device attribute structure.
  1314. * @buf: Pointer to the buffer location for the configuration
  1315. * data.
  1316. * returns: size of the buffer.
  1317. */
  1318. static ssize_t xdevcfg_show_is_partial_bitstream_status(struct device *dev,
  1319. struct device_attribute *attr, char *buf)
  1320. {
  1321. ssize_t status;
  1322. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1323. status = sprintf(buf, "%d\n", drvdata->is_partial_bitstream);
  1324. return status;
  1325. }
  1326. static DEVICE_ATTR(is_partial_bitstream, 0644,
  1327. xdevcfg_show_is_partial_bitstream_status,
  1328. xdevcfg_set_is_partial_bitstream);
  1329. static const struct attribute *xdevcfg_attrs[] = {
  1330. &dev_attr_prog_done.attr, /* PCFG_DONE bit in Intr Status register */
  1331. &dev_attr_dbg_lock.attr, /* Debug lock bit in Lock register */
  1332. &dev_attr_seu_lock.attr, /* SEU lock bit in Lock register */
  1333. &dev_attr_aes_en_lock.attr, /* AES EN lock bit in Lock register */
  1334. &dev_attr_enable_aes.attr, /* AES EN bit in Control register */
  1335. &dev_attr_enable_seu.attr, /* SEU EN bit in Control register */
  1336. &dev_attr_enable_sec_dbg_nonin.attr, /*SPNIDEN bit in Control register*/
  1337. &dev_attr_enable_sec_dbg_in.attr, /*SPIDEN bit in Control register */
  1338. &dev_attr_enable_dbg_nonin.attr, /* NIDEN bit in Control register */
  1339. &dev_attr_enable_dbg_in.attr, /* DBGEN bit in Control register */
  1340. &dev_attr_enable_dap.attr, /* DAP_EN bits in Control register */
  1341. &dev_attr_is_partial_bitstream.attr, /* Flag for partial bitstream */
  1342. NULL,
  1343. };
  1344. static const struct attribute_group xdevcfg_attr_group = {
  1345. .attrs = (struct attribute **) xdevcfg_attrs,
  1346. };
  1347. static ssize_t fclk_enable_show(struct device *dev,
  1348. struct device_attribute *attr, char *buf)
  1349. {
  1350. struct fclk_data *pdata = dev_get_drvdata(dev);
  1351. return scnprintf(buf, PAGE_SIZE, "%u\n", pdata->enabled);
  1352. }
  1353. static ssize_t fclk_enable_store(struct device *dev,
  1354. struct device_attribute *attr, const char *buf, size_t count)
  1355. {
  1356. unsigned long enable;
  1357. int ret;
  1358. struct fclk_data *pdata = dev_get_drvdata(dev);
  1359. ret = kstrtoul(buf, 0, &enable);
  1360. if (ret)
  1361. return -EINVAL;
  1362. enable = !!enable;
  1363. if (enable == pdata->enabled)
  1364. return count;
  1365. if (enable)
  1366. ret = clk_enable(pdata->clk);
  1367. else
  1368. clk_disable(pdata->clk);
  1369. if (ret)
  1370. return ret;
  1371. pdata->enabled = enable;
  1372. return count;
  1373. }
  1374. static DEVICE_ATTR(enable, 0644, fclk_enable_show, fclk_enable_store);
  1375. static ssize_t fclk_set_rate_show(struct device *dev,
  1376. struct device_attribute *attr, char *buf)
  1377. {
  1378. struct fclk_data *pdata = dev_get_drvdata(dev);
  1379. return scnprintf(buf, PAGE_SIZE, "%lu\n", clk_get_rate(pdata->clk));
  1380. }
  1381. static ssize_t fclk_set_rate_store(struct device *dev,
  1382. struct device_attribute *attr, const char *buf, size_t count)
  1383. {
  1384. int ret = 0;
  1385. unsigned long rate;
  1386. struct fclk_data *pdata = dev_get_drvdata(dev);
  1387. ret = kstrtoul(buf, 0, &rate);
  1388. if (ret)
  1389. return -EINVAL;
  1390. rate = clk_round_rate(pdata->clk, rate);
  1391. ret = clk_set_rate(pdata->clk, rate);
  1392. return ret ? ret : count;
  1393. }
  1394. static DEVICE_ATTR(set_rate, 0644, fclk_set_rate_show, fclk_set_rate_store);
  1395. static ssize_t fclk_round_rate_show(struct device *dev,
  1396. struct device_attribute *attr, char *buf)
  1397. {
  1398. struct fclk_data *pdata = dev_get_drvdata(dev);
  1399. return scnprintf(buf, PAGE_SIZE, "%lu => %lu\n", pdata->rate_rnd,
  1400. clk_round_rate(pdata->clk, pdata->rate_rnd));
  1401. }
  1402. static ssize_t fclk_round_rate_store(struct device *dev,
  1403. struct device_attribute *attr, const char *buf, size_t count)
  1404. {
  1405. int ret = 0;
  1406. unsigned long rate;
  1407. struct fclk_data *pdata = dev_get_drvdata(dev);
  1408. ret = kstrtoul(buf, 0, &rate);
  1409. if (ret)
  1410. return -EINVAL;
  1411. pdata->rate_rnd = rate;
  1412. return count;
  1413. }
  1414. static DEVICE_ATTR(round_rate, 0644, fclk_round_rate_show,
  1415. fclk_round_rate_store);
  1416. static const struct attribute *fclk_ctrl_attrs[] = {
  1417. &dev_attr_enable.attr,
  1418. &dev_attr_set_rate.attr,
  1419. &dev_attr_round_rate.attr,
  1420. NULL,
  1421. };
  1422. static const struct attribute_group fclk_ctrl_attr_grp = {
  1423. .attrs = (struct attribute **)fclk_ctrl_attrs,
  1424. };
  1425. static ssize_t xdevcfg_fclk_export_store(struct device *dev,
  1426. struct device_attribute *attr, const char *buf, size_t size)
  1427. {
  1428. int i, ret;
  1429. struct device *subdev;
  1430. struct fclk_data *fdata;
  1431. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1432. for (i = 0; i < NUMFCLKS; i++) {
  1433. if (!strncmp(buf, fclk_name[i], strlen(fclk_name[i])))
  1434. break;
  1435. }
  1436. if (i < NUMFCLKS && !drvdata->fclk_exported[i]) {
  1437. drvdata->fclk_exported[i] = 1;
  1438. subdev = device_create(drvdata->fclk_class, dev, MKDEV(0, 0),
  1439. NULL, fclk_name[i]);
  1440. if (IS_ERR(subdev))
  1441. return PTR_ERR(subdev);
  1442. ret = clk_prepare(drvdata->fclk[i]);
  1443. if (ret)
  1444. return ret;
  1445. fdata = kzalloc(sizeof(*fdata), GFP_KERNEL);
  1446. if (!fdata) {
  1447. ret = -ENOMEM;
  1448. goto err_unprepare;
  1449. }
  1450. fdata->clk = drvdata->fclk[i];
  1451. dev_set_drvdata(subdev, fdata);
  1452. ret = sysfs_create_group(&subdev->kobj, &fclk_ctrl_attr_grp);
  1453. if (ret)
  1454. goto err_free;
  1455. } else {
  1456. return -EINVAL;
  1457. }
  1458. return size;
  1459. err_free:
  1460. kfree(fdata);
  1461. err_unprepare:
  1462. clk_unprepare(drvdata->fclk[i]);
  1463. return ret;
  1464. }
  1465. static ssize_t xdevcfg_fclk_export_show(struct device *dev,
  1466. struct device_attribute *attr, char *buf)
  1467. {
  1468. int i;
  1469. ssize_t count = 0;
  1470. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1471. for (i = 0; i < NUMFCLKS; i++) {
  1472. if (!drvdata->fclk_exported[i])
  1473. count += scnprintf(buf + count, PAGE_SIZE - count,
  1474. "%s\n", fclk_name[i]);
  1475. }
  1476. return count;
  1477. }
  1478. static DEVICE_ATTR(fclk_export, 0644, xdevcfg_fclk_export_show,
  1479. xdevcfg_fclk_export_store);
  1480. static int match_fclk(struct device *dev, const void *data)
  1481. {
  1482. struct fclk_data *fdata = dev_get_drvdata(dev);
  1483. return fdata->clk == data;
  1484. }
  1485. static ssize_t xdevcfg_fclk_unexport_store(struct device *dev,
  1486. struct device_attribute *attr, const char *buf, size_t size)
  1487. {
  1488. int i;
  1489. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1490. for (i = 0; i < NUMFCLKS; i++) {
  1491. if (!strncmp(buf, fclk_name[i], strlen(fclk_name[i])))
  1492. break;
  1493. }
  1494. if (i < NUMFCLKS && drvdata->fclk_exported[i]) {
  1495. struct fclk_data *fdata;
  1496. struct device *subdev;
  1497. drvdata->fclk_exported[i] = 0;
  1498. subdev = class_find_device(drvdata->fclk_class, NULL,
  1499. drvdata->fclk[i], match_fclk);
  1500. fdata = dev_get_drvdata(subdev);
  1501. if (fdata->enabled)
  1502. clk_disable(fdata->clk);
  1503. clk_unprepare(fdata->clk);
  1504. kfree(fdata);
  1505. device_unregister(subdev);
  1506. put_device(subdev);
  1507. } else {
  1508. return -EINVAL;
  1509. }
  1510. return size;
  1511. }
  1512. static ssize_t xdevcfg_fclk_unexport_show(struct device *dev,
  1513. struct device_attribute *attr, char *buf)
  1514. {
  1515. int i;
  1516. ssize_t count = 0;
  1517. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1518. for (i = 0; i < NUMFCLKS; i++) {
  1519. if (drvdata->fclk_exported[i])
  1520. count += scnprintf(buf + count, PAGE_SIZE - count,
  1521. "%s\n", fclk_name[i]);
  1522. }
  1523. return count;
  1524. }
  1525. static DEVICE_ATTR(fclk_unexport, 0644, xdevcfg_fclk_unexport_show,
  1526. xdevcfg_fclk_unexport_store);
  1527. static const struct attribute *fclk_exp_attrs[] = {
  1528. &dev_attr_fclk_export.attr,
  1529. &dev_attr_fclk_unexport.attr,
  1530. NULL,
  1531. };
  1532. static const struct attribute_group fclk_exp_attr_grp = {
  1533. .attrs = (struct attribute **)fclk_exp_attrs,
  1534. };
  1535. static void xdevcfg_fclk_init(struct device *dev)
  1536. {
  1537. int i;
  1538. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1539. for (i = 0; i < NUMFCLKS; i++) {
  1540. drvdata->fclk[i] = clk_get(dev, fclk_name[i]);
  1541. if (IS_ERR(drvdata->fclk[i])) {
  1542. dev_warn(dev, "fclk not found\n");
  1543. return;
  1544. }
  1545. }
  1546. drvdata->fclk_class = class_create("fclk");
  1547. if (IS_ERR(drvdata->fclk_class)) {
  1548. dev_warn(dev, "failed to create fclk class\n");
  1549. return;
  1550. }
  1551. if (sysfs_create_group(&dev->kobj, &fclk_exp_attr_grp))
  1552. dev_warn(dev, "failed to create sysfs entries\n");
  1553. }
  1554. static void xdevcfg_fclk_remove(struct device *dev)
  1555. {
  1556. int i;
  1557. struct xdevcfg_drvdata *drvdata = dev_get_drvdata(dev);
  1558. for (i = 0; i < NUMFCLKS; i++) {
  1559. if (drvdata->fclk_exported[i]) {
  1560. struct fclk_data *fdata;
  1561. struct device *subdev;
  1562. drvdata->fclk_exported[i] = 0;
  1563. subdev = class_find_device(drvdata->fclk_class, NULL,
  1564. drvdata->fclk[i], match_fclk);
  1565. fdata = dev_get_drvdata(subdev);
  1566. if (fdata->enabled)
  1567. clk_disable(fdata->clk);
  1568. clk_unprepare(fdata->clk);
  1569. kfree(fdata);
  1570. device_unregister(subdev);
  1571. put_device(subdev);
  1572. }
  1573. }
  1574. class_destroy(drvdata->fclk_class);
  1575. sysfs_remove_group(&dev->kobj, &fclk_exp_attr_grp);
  1576. }
  1577. /**
  1578. * xdevcfg_drv_probe - Probe call for the device.
  1579. *
  1580. * @pdev: handle to the platform device structure.
  1581. *
  1582. * Returns: 0 on success, negative error otherwise.
  1583. *
  1584. * It does all the memory allocation and registration for the device.
  1585. */
  1586. static int xdevcfg_drv_probe(struct platform_device *pdev)
  1587. {
  1588. struct resource *res;
  1589. struct xdevcfg_drvdata *drvdata;
  1590. dev_t devt;
  1591. int retval;
  1592. u32 ctrlreg;
  1593. struct device_node *np;
  1594. const void *prop;
  1595. int size;
  1596. struct device *dev;
  1597. zynq_slcr_regmap = syscon_regmap_lookup_by_compatible("xlnx,zynq-slcr");
  1598. if (IS_ERR(zynq_slcr_regmap)) {
  1599. pr_err("%s: failed to find zynq-slcr\n", __func__);
  1600. return -ENODEV;
  1601. }
  1602. drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
  1603. if (!drvdata)
  1604. return -ENOMEM;
  1605. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1606. drvdata->base_address = devm_ioremap_resource(&pdev->dev, res);
  1607. if (IS_ERR(drvdata->base_address))
  1608. return PTR_ERR(drvdata->base_address);
  1609. drvdata->irq = platform_get_irq(pdev, 0);
  1610. retval = devm_request_irq(&pdev->dev, drvdata->irq, &xdevcfg_irq,
  1611. 0, dev_name(&pdev->dev), drvdata);
  1612. if (retval) {
  1613. dev_err(&pdev->dev, "No IRQ available");
  1614. return retval;
  1615. }
  1616. platform_set_drvdata(pdev, drvdata);
  1617. spin_lock_init(&drvdata->lock);
  1618. mutex_init(&drvdata->sem);
  1619. drvdata->is_open = 0;
  1620. drvdata->is_partial_bitstream = 0;
  1621. drvdata->dma_done = 0;
  1622. drvdata->error_status = 0;
  1623. dev_info(&pdev->dev, "ioremap %pa to %p\n",
  1624. &res->start, drvdata->base_address);
  1625. drvdata->clk = devm_clk_get(&pdev->dev, "ref_clk");
  1626. if (IS_ERR(drvdata->clk)) {
  1627. dev_err(&pdev->dev, "input clock not found\n");
  1628. return PTR_ERR(drvdata->clk);
  1629. }
  1630. retval = clk_prepare_enable(drvdata->clk);
  1631. if (retval) {
  1632. dev_err(&pdev->dev, "unable to enable clock\n");
  1633. return retval;
  1634. }
  1635. /*
  1636. * Figure out from the device tree if this is running on the EP107
  1637. * emulation platform as it doesn't match the silicon exactly and the
  1638. * driver needs to work accordingly.
  1639. */
  1640. np = of_get_next_parent(pdev->dev.of_node);
  1641. np = of_get_next_parent(np);
  1642. prop = of_get_property(np, "compatible", &size);
  1643. if (prop != NULL) {
  1644. if ((strcmp((const char *)prop, "xlnx,zynq-ep107")) == 0)
  1645. drvdata->ep107 = 1;
  1646. else
  1647. drvdata->ep107 = 0;
  1648. }
  1649. /* Unlock the device */
  1650. xdevcfg_writereg(drvdata->base_address + XDCFG_UNLOCK_OFFSET,
  1651. 0x757BDF0D);
  1652. /*
  1653. * Set the configuration register with the following options
  1654. * - Reset FPGA
  1655. * - Enable the PCAP interface
  1656. * - Set the throughput rate for maximum speed
  1657. * - Set the CPU in user mode
  1658. */
  1659. ctrlreg = xdevcfg_readreg(drvdata->base_address + XDCFG_CTRL_OFFSET);
  1660. ctrlreg &= ~XDCFG_CTRL_PCAP_PR_MASK;
  1661. ctrlreg |= XDCFG_CTRL_PCFG_PROG_B_MASK | XDCFG_CTRL_PCAP_MODE_MASK;
  1662. xdevcfg_writereg(drvdata->base_address + XDCFG_CTRL_OFFSET, ctrlreg);
  1663. /* Ensure internal PCAP loopback is disabled */
  1664. ctrlreg = xdevcfg_readreg(drvdata->base_address + XDCFG_MCTRL_OFFSET);
  1665. xdevcfg_writereg(drvdata->base_address + XDCFG_MCTRL_OFFSET,
  1666. (~XDCFG_MCTRL_PCAP_LPBK_MASK &
  1667. ctrlreg));
  1668. retval = alloc_chrdev_region(&devt, 0, XDEVCFG_DEVICES, DRIVER_NAME);
  1669. if (retval < 0)
  1670. goto failed5;
  1671. drvdata->devt = devt;
  1672. cdev_init(&drvdata->cdev, &xdevcfg_fops);
  1673. drvdata->cdev.owner = THIS_MODULE;
  1674. retval = cdev_add(&drvdata->cdev, devt, 1);
  1675. if (retval) {
  1676. dev_err(&pdev->dev, "cdev_add() failed\n");
  1677. goto failed6;
  1678. }
  1679. drvdata->class = class_create(DRIVER_NAME);
  1680. if (IS_ERR(drvdata->class)) {
  1681. dev_err(&pdev->dev, "failed to create class\n");
  1682. goto failed6;
  1683. }
  1684. dev = device_create(drvdata->class, &pdev->dev, devt, drvdata,
  1685. DRIVER_NAME);
  1686. if (IS_ERR(dev)) {
  1687. dev_err(&pdev->dev, "unable to create device\n");
  1688. goto failed7;
  1689. }
  1690. drvdata->dev = &pdev->dev;
  1691. /* create sysfs files for the device */
  1692. retval = sysfs_create_group(&(pdev->dev.kobj), &xdevcfg_attr_group);
  1693. if (retval) {
  1694. dev_err(&pdev->dev, "Failed to create sysfs attr group\n");
  1695. cdev_del(&drvdata->cdev);
  1696. goto failed8;
  1697. }
  1698. xdevcfg_fclk_init(&pdev->dev);
  1699. clk_disable(drvdata->clk);
  1700. return 0; /* Success */
  1701. failed8:
  1702. device_destroy(drvdata->class, drvdata->devt);
  1703. failed7:
  1704. class_destroy(drvdata->class);
  1705. failed6:
  1706. /* Unregister char driver */
  1707. unregister_chrdev_region(devt, XDEVCFG_DEVICES);
  1708. failed5:
  1709. clk_disable_unprepare(drvdata->clk);
  1710. return retval;
  1711. }
  1712. /**
  1713. * xdevcfg_drv_remove - Remove call for the device.
  1714. *
  1715. * @pdev: handle to the platform device structure.
  1716. *
  1717. * Unregister the device after releasing the resources.
  1718. */
  1719. static void xdevcfg_drv_remove(struct platform_device *pdev)
  1720. {
  1721. struct xdevcfg_drvdata *drvdata;
  1722. drvdata = platform_get_drvdata(pdev);
  1723. if (!drvdata)
  1724. return;
  1725. unregister_chrdev_region(drvdata->devt, XDEVCFG_DEVICES);
  1726. sysfs_remove_group(&pdev->dev.kobj, &xdevcfg_attr_group);
  1727. xdevcfg_fclk_remove(&pdev->dev);
  1728. device_destroy(drvdata->class, drvdata->devt);
  1729. class_destroy(drvdata->class);
  1730. cdev_del(&drvdata->cdev);
  1731. clk_unprepare(drvdata->clk);
  1732. }
  1733. static const struct of_device_id xdevcfg_of_match[] = {
  1734. { .compatible = "xlnx,zynq-devcfg-1.0", },
  1735. { /* end of table */}
  1736. };
  1737. MODULE_DEVICE_TABLE(of, xdevcfg_of_match);
  1738. /* Driver Structure */
  1739. static struct platform_driver xdevcfg_platform_driver = {
  1740. .probe = xdevcfg_drv_probe,
  1741. .remove = xdevcfg_drv_remove,
  1742. .driver = {
  1743. .owner = THIS_MODULE,
  1744. .name = DRIVER_NAME,
  1745. .of_match_table = xdevcfg_of_match,
  1746. },
  1747. };
  1748. module_platform_driver(xdevcfg_platform_driver);
  1749. MODULE_AUTHOR("Xilinx, Inc");
  1750. MODULE_DESCRIPTION("Xilinx Device Config Driver");
  1751. MODULE_LICENSE("GPL");