xilinx_hwicap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*****************************************************************************
  2. *
  3. * Author: Xilinx, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
  11. * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
  12. * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
  13. * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
  14. * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
  15. * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
  16. * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
  17. * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
  18. * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
  19. * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
  20. * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
  21. * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE.
  23. *
  24. * Xilinx products are not intended for use in life support appliances,
  25. * devices, or systems. Use in such applications is expressly prohibited.
  26. *
  27. * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group
  28. * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group
  29. * (c) Copyright 2007-2008 Xilinx Inc.
  30. * All rights reserved.
  31. *
  32. * You should have received a copy of the GNU General Public License along
  33. * with this program; if not, write to the Free Software Foundation, Inc.,
  34. * 675 Mass Ave, Cambridge, MA 02139, USA.
  35. *
  36. *****************************************************************************/
  37. /*
  38. * This is the code behind /dev/xilinx_icap -- it allows a user-space
  39. * application to use the Xilinx ICAP subsystem.
  40. *
  41. * The following operations are possible:
  42. *
  43. * open open the port and initialize for access.
  44. * release release port
  45. * write Write a bitstream to the configuration processor.
  46. * read Read a data stream from the configuration processor.
  47. *
  48. * After being opened, the port is initialized and accessed to avoid a
  49. * corrupted first read which may occur with some hardware. The port
  50. * is left in a desynched state, requiring that a synch sequence be
  51. * transmitted before any valid configuration data. A user will have
  52. * exclusive access to the device while it remains open, and the state
  53. * of the ICAP cannot be guaranteed after the device is closed. Note
  54. * that a complete reset of the core and the state of the ICAP cannot
  55. * be performed on many versions of the cores, hence users of this
  56. * device should avoid making inconsistent accesses to the device. In
  57. * particular, accessing the read interface, without first generating
  58. * a write containing a readback packet can leave the ICAP in an
  59. * inaccessible state.
  60. *
  61. * Note that in order to use the read interface, it is first necessary
  62. * to write a request packet to the write interface. i.e., it is not
  63. * possible to simply readback the bitstream (or any configuration
  64. * bits) from a device without specifically requesting them first.
  65. * The code to craft such packets is intended to be part of the
  66. * user-space application code that uses this device. The simplest
  67. * way to use this interface is simply:
  68. *
  69. * cp foo.bit /dev/xilinx_icap
  70. *
  71. * Note that unless foo.bit is an appropriately constructed partial
  72. * bitstream, this has a high likelyhood of overwriting the design
  73. * currently programmed in the FPGA.
  74. */
  75. #include <linux/version.h>
  76. #include <linux/module.h>
  77. #include <linux/kernel.h>
  78. #include <linux/types.h>
  79. #include <linux/ioport.h>
  80. #include <linux/interrupt.h>
  81. #include <linux/fcntl.h>
  82. #include <linux/init.h>
  83. #include <linux/poll.h>
  84. #include <linux/proc_fs.h>
  85. #include <asm/semaphore.h>
  86. #include <linux/sysctl.h>
  87. #include <linux/version.h>
  88. #include <linux/fs.h>
  89. #include <linux/cdev.h>
  90. #include <linux/platform_device.h>
  91. #include <asm/io.h>
  92. #include <asm/uaccess.h>
  93. #include <asm/system.h>
  94. #ifdef CONFIG_OF
  95. /* For open firmware. */
  96. #include <linux/of_device.h>
  97. #include <linux/of_platform.h>
  98. #endif
  99. #include "xilinx_hwicap.h"
  100. #include "buffer_icap.h"
  101. #include "fifo_icap.h"
  102. #define DRIVER_NAME "xilinx_icap"
  103. #define HWICAP_REGS (0x10000)
  104. /* dynamically allocate device number */
  105. static int xhwicap_major;
  106. static int xhwicap_minor;
  107. #define HWICAP_DEVICES 1
  108. module_param(xhwicap_major, int, S_IRUGO);
  109. module_param(xhwicap_minor, int, S_IRUGO);
  110. /* An array, which is set to true when the device is registered. */
  111. static bool probed_devices[HWICAP_DEVICES];
  112. static struct class *icap_class;
  113. #define UNIMPLEMENTED 0xFFFF
  114. static const struct config_registers v2_config_registers = {
  115. .CRC = 0,
  116. .FAR = 1,
  117. .FDRI = 2,
  118. .FDRO = 3,
  119. .CMD = 4,
  120. .CTL = 5,
  121. .MASK = 6,
  122. .STAT = 7,
  123. .LOUT = 8,
  124. .COR = 9,
  125. .MFWR = 10,
  126. .FLR = 11,
  127. .KEY = 12,
  128. .CBC = 13,
  129. .IDCODE = 14,
  130. .AXSS = UNIMPLEMENTED,
  131. .C0R_1 = UNIMPLEMENTED,
  132. .CSOB = UNIMPLEMENTED,
  133. .WBSTAR = UNIMPLEMENTED,
  134. .TIMER = UNIMPLEMENTED,
  135. .BOOTSTS = UNIMPLEMENTED,
  136. .CTL_1 = UNIMPLEMENTED,
  137. };
  138. static const struct config_registers v4_config_registers = {
  139. .CRC = 0,
  140. .FAR = 1,
  141. .FDRI = 2,
  142. .FDRO = 3,
  143. .CMD = 4,
  144. .CTL = 5,
  145. .MASK = 6,
  146. .STAT = 7,
  147. .LOUT = 8,
  148. .COR = 9,
  149. .MFWR = 10,
  150. .FLR = UNIMPLEMENTED,
  151. .KEY = UNIMPLEMENTED,
  152. .CBC = 11,
  153. .IDCODE = 12,
  154. .AXSS = 13,
  155. .C0R_1 = UNIMPLEMENTED,
  156. .CSOB = UNIMPLEMENTED,
  157. .WBSTAR = UNIMPLEMENTED,
  158. .TIMER = UNIMPLEMENTED,
  159. .BOOTSTS = UNIMPLEMENTED,
  160. .CTL_1 = UNIMPLEMENTED,
  161. };
  162. static const struct config_registers v5_config_registers = {
  163. .CRC = 0,
  164. .FAR = 1,
  165. .FDRI = 2,
  166. .FDRO = 3,
  167. .CMD = 4,
  168. .CTL = 5,
  169. .MASK = 6,
  170. .STAT = 7,
  171. .LOUT = 8,
  172. .COR = 9,
  173. .MFWR = 10,
  174. .FLR = UNIMPLEMENTED,
  175. .KEY = UNIMPLEMENTED,
  176. .CBC = 11,
  177. .IDCODE = 12,
  178. .AXSS = 13,
  179. .C0R_1 = 14,
  180. .CSOB = 15,
  181. .WBSTAR = 16,
  182. .TIMER = 17,
  183. .BOOTSTS = 18,
  184. .CTL_1 = 19,
  185. };
  186. /**
  187. * hwicap_command_desync: Send a DESYNC command to the ICAP port.
  188. * @parameter drvdata: a pointer to the drvdata.
  189. *
  190. * This command desynchronizes the ICAP After this command, a
  191. * bitstream containing a NULL packet, followed by a SYNCH packet is
  192. * required before the ICAP will recognize commands.
  193. */
  194. int hwicap_command_desync(struct hwicap_drvdata *drvdata)
  195. {
  196. u32 buffer[4];
  197. u32 index = 0;
  198. /*
  199. * Create the data to be written to the ICAP.
  200. */
  201. buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
  202. buffer[index++] = XHI_CMD_DESYNCH;
  203. buffer[index++] = XHI_NOOP_PACKET;
  204. buffer[index++] = XHI_NOOP_PACKET;
  205. /*
  206. * Write the data to the FIFO and intiate the transfer of data present
  207. * in the FIFO to the ICAP device.
  208. */
  209. return drvdata->config->set_configuration(drvdata,
  210. &buffer[0], index);
  211. }
  212. /**
  213. * hwicap_command_capture: Send a CAPTURE command to the ICAP port.
  214. * @parameter drvdata: a pointer to the drvdata.
  215. *
  216. * This command captures all of the flip flop states so they will be
  217. * available during readback. One can use this command instead of
  218. * enabling the CAPTURE block in the design.
  219. */
  220. int hwicap_command_capture(struct hwicap_drvdata *drvdata)
  221. {
  222. u32 buffer[7];
  223. u32 index = 0;
  224. /*
  225. * Create the data to be written to the ICAP.
  226. */
  227. buffer[index++] = XHI_DUMMY_PACKET;
  228. buffer[index++] = XHI_SYNC_PACKET;
  229. buffer[index++] = XHI_NOOP_PACKET;
  230. buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
  231. buffer[index++] = XHI_CMD_GCAPTURE;
  232. buffer[index++] = XHI_DUMMY_PACKET;
  233. buffer[index++] = XHI_DUMMY_PACKET;
  234. /*
  235. * Write the data to the FIFO and intiate the transfer of data
  236. * present in the FIFO to the ICAP device.
  237. */
  238. return drvdata->config->set_configuration(drvdata,
  239. &buffer[0], index);
  240. }
  241. /**
  242. * hwicap_get_configuration_register: Query a configuration register.
  243. * @parameter drvdata: a pointer to the drvdata.
  244. * @parameter reg: a constant which represents the configuration
  245. * register value to be returned.
  246. * Examples: XHI_IDCODE, XHI_FLR.
  247. * @parameter RegData: returns the value of the register.
  248. *
  249. * Sends a query packet to the ICAP and then receives the response.
  250. * The icap is left in Synched state.
  251. */
  252. int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
  253. u32 reg, u32 *RegData)
  254. {
  255. int status;
  256. u32 buffer[6];
  257. u32 index = 0;
  258. /*
  259. * Create the data to be written to the ICAP.
  260. */
  261. buffer[index++] = XHI_DUMMY_PACKET;
  262. buffer[index++] = XHI_SYNC_PACKET;
  263. buffer[index++] = XHI_NOOP_PACKET;
  264. buffer[index++] = hwicap_type_1_read(reg) | 1;
  265. buffer[index++] = XHI_NOOP_PACKET;
  266. buffer[index++] = XHI_NOOP_PACKET;
  267. /*
  268. * Write the data to the FIFO and intiate the transfer of data present
  269. * in the FIFO to the ICAP device.
  270. */
  271. status = drvdata->config->set_configuration(drvdata,
  272. &buffer[0], index);
  273. if (status)
  274. return status;
  275. /*
  276. * Read the configuration register
  277. */
  278. status = drvdata->config->get_configuration(drvdata, RegData, 1);
  279. if (status)
  280. return status;
  281. return 0;
  282. }
  283. int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
  284. {
  285. int status;
  286. u32 idcode;
  287. dev_dbg(drvdata->dev, "initializing\n");
  288. /* Abort any current transaction, to make sure we have the
  289. * ICAP in a good state. */
  290. dev_dbg(drvdata->dev, "Reset...\n");
  291. drvdata->config->reset(drvdata);
  292. dev_dbg(drvdata->dev, "Desync...\n");
  293. status = hwicap_command_desync(drvdata);
  294. if (status)
  295. return status;
  296. /* Attempt to read the IDCODE from ICAP. This
  297. * may not be returned correctly, due to the design of the
  298. * hardware.
  299. */
  300. dev_dbg(drvdata->dev, "Reading IDCODE...\n");
  301. status = hwicap_get_configuration_register(
  302. drvdata, drvdata->config_regs->IDCODE, &idcode);
  303. dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
  304. if (status)
  305. return status;
  306. dev_dbg(drvdata->dev, "Desync...\n");
  307. status = hwicap_command_desync(drvdata);
  308. if (status)
  309. return status;
  310. return 0;
  311. }
  312. static ssize_t
  313. hwicap_read(struct file *file, char *buf, size_t count, loff_t *ppos)
  314. {
  315. struct hwicap_drvdata *drvdata = file->private_data;
  316. ssize_t bytes_to_read = 0;
  317. u32 *kbuf;
  318. u32 words;
  319. u32 bytes_remaining;
  320. int status;
  321. if (down_interruptible(&drvdata->sem))
  322. return -ERESTARTSYS;
  323. if (drvdata->read_buffer_in_use) {
  324. /* If there are leftover bytes in the buffer, just */
  325. /* return them and don't try to read more from the */
  326. /* ICAP device. */
  327. bytes_to_read =
  328. (count < drvdata->read_buffer_in_use) ? count :
  329. drvdata->read_buffer_in_use;
  330. /* Return the data currently in the read buffer. */
  331. if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
  332. status = -EFAULT;
  333. goto error;
  334. }
  335. drvdata->read_buffer_in_use -= bytes_to_read;
  336. memcpy(drvdata->read_buffer + bytes_to_read,
  337. drvdata->read_buffer, 4 - bytes_to_read);
  338. } else {
  339. /* Get new data from the ICAP, and return was was requested. */
  340. kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
  341. if (!kbuf) {
  342. status = -ENOMEM;
  343. goto error;
  344. }
  345. /* The ICAP device is only able to read complete */
  346. /* words. If a number of bytes that do not correspond */
  347. /* to complete words is requested, then we read enough */
  348. /* words to get the required number of bytes, and then */
  349. /* save the remaining bytes for the next read. */
  350. /* Determine the number of words to read, rounding up */
  351. /* if necessary. */
  352. words = ((count + 3) >> 2);
  353. bytes_to_read = words << 2;
  354. if (bytes_to_read > PAGE_SIZE)
  355. bytes_to_read = PAGE_SIZE;
  356. /* Ensure we only read a complete number of words. */
  357. bytes_remaining = bytes_to_read & 3;
  358. bytes_to_read &= ~3;
  359. words = bytes_to_read >> 2;
  360. status = drvdata->config->get_configuration(drvdata,
  361. kbuf, words);
  362. /* If we didn't read correctly, then bail out. */
  363. if (status) {
  364. free_page((unsigned long)kbuf);
  365. goto error;
  366. }
  367. /* If we fail to return the data to the user, then bail out. */
  368. if (copy_to_user(buf, kbuf, bytes_to_read)) {
  369. free_page((unsigned long)kbuf);
  370. status = -EFAULT;
  371. goto error;
  372. }
  373. memcpy(kbuf, drvdata->read_buffer, bytes_remaining);
  374. drvdata->read_buffer_in_use = bytes_remaining;
  375. free_page((unsigned long)kbuf);
  376. }
  377. status = bytes_to_read;
  378. error:
  379. up(&drvdata->sem);
  380. return status;
  381. }
  382. static ssize_t
  383. hwicap_write(struct file *file, const char *buf,
  384. size_t count, loff_t *ppos)
  385. {
  386. struct hwicap_drvdata *drvdata = file->private_data;
  387. ssize_t written = 0;
  388. ssize_t left = count;
  389. u32 *kbuf;
  390. ssize_t len;
  391. ssize_t status;
  392. if (down_interruptible(&drvdata->sem))
  393. return -ERESTARTSYS;
  394. left += drvdata->write_buffer_in_use;
  395. /* Only write multiples of 4 bytes. */
  396. if (left < 4) {
  397. status = 0;
  398. goto error;
  399. }
  400. kbuf = (u32 *) __get_free_page(GFP_KERNEL);
  401. if (!kbuf) {
  402. status = -ENOMEM;
  403. goto error;
  404. }
  405. while (left > 3) {
  406. /* only write multiples of 4 bytes, so there might */
  407. /* be as many as 3 bytes left (at the end). */
  408. len = left;
  409. if (len > PAGE_SIZE)
  410. len = PAGE_SIZE;
  411. len &= ~3;
  412. if (drvdata->write_buffer_in_use) {
  413. memcpy(kbuf, drvdata->write_buffer,
  414. drvdata->write_buffer_in_use);
  415. if (copy_from_user(
  416. (((char *)kbuf) + (drvdata->write_buffer_in_use)),
  417. buf + written,
  418. len - (drvdata->write_buffer_in_use))) {
  419. free_page((unsigned long)kbuf);
  420. status = -EFAULT;
  421. goto error;
  422. }
  423. } else {
  424. if (copy_from_user(kbuf, buf + written, len)) {
  425. free_page((unsigned long)kbuf);
  426. status = -EFAULT;
  427. goto error;
  428. }
  429. }
  430. status = drvdata->config->set_configuration(drvdata,
  431. kbuf, len >> 2);
  432. if (status) {
  433. free_page((unsigned long)kbuf);
  434. status = -EFAULT;
  435. goto error;
  436. }
  437. if (drvdata->write_buffer_in_use) {
  438. len -= drvdata->write_buffer_in_use;
  439. left -= drvdata->write_buffer_in_use;
  440. drvdata->write_buffer_in_use = 0;
  441. }
  442. written += len;
  443. left -= len;
  444. }
  445. if ((left > 0) && (left < 4)) {
  446. if (!copy_from_user(drvdata->write_buffer,
  447. buf + written, left)) {
  448. drvdata->write_buffer_in_use = left;
  449. written += left;
  450. left = 0;
  451. }
  452. }
  453. free_page((unsigned long)kbuf);
  454. status = written;
  455. error:
  456. up(&drvdata->sem);
  457. return status;
  458. }
  459. static int hwicap_open(struct inode *inode, struct file *file)
  460. {
  461. struct hwicap_drvdata *drvdata;
  462. int status;
  463. drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
  464. if (down_interruptible(&drvdata->sem))
  465. return -ERESTARTSYS;
  466. if (drvdata->is_open) {
  467. status = -EBUSY;
  468. goto error;
  469. }
  470. status = hwicap_initialize_hwicap(drvdata);
  471. if (status) {
  472. dev_err(drvdata->dev, "Failed to open file");
  473. goto error;
  474. }
  475. file->private_data = drvdata;
  476. drvdata->write_buffer_in_use = 0;
  477. drvdata->read_buffer_in_use = 0;
  478. drvdata->is_open = 1;
  479. error:
  480. up(&drvdata->sem);
  481. return status;
  482. }
  483. static int hwicap_release(struct inode *inode, struct file *file)
  484. {
  485. struct hwicap_drvdata *drvdata = file->private_data;
  486. int i;
  487. int status = 0;
  488. if (down_interruptible(&drvdata->sem))
  489. return -ERESTARTSYS;
  490. if (drvdata->write_buffer_in_use) {
  491. /* Flush write buffer. */
  492. for (i = drvdata->write_buffer_in_use; i < 4; i++)
  493. drvdata->write_buffer[i] = 0;
  494. status = drvdata->config->set_configuration(drvdata,
  495. (u32 *) drvdata->write_buffer, 1);
  496. if (status)
  497. goto error;
  498. }
  499. status = hwicap_command_desync(drvdata);
  500. if (status)
  501. goto error;
  502. error:
  503. drvdata->is_open = 0;
  504. up(&drvdata->sem);
  505. return status;
  506. }
  507. static struct file_operations hwicap_fops = {
  508. .owner = THIS_MODULE,
  509. .write = hwicap_write,
  510. .read = hwicap_read,
  511. .open = hwicap_open,
  512. .release = hwicap_release,
  513. };
  514. static int __devinit hwicap_setup(struct device *dev, int id,
  515. const struct resource *regs_res,
  516. const struct hwicap_driver_config *config,
  517. const struct config_registers *config_regs)
  518. {
  519. dev_t devt;
  520. struct hwicap_drvdata *drvdata = NULL;
  521. int retval = 0;
  522. dev_info(dev, "Xilinx icap port driver\n");
  523. if (id < 0) {
  524. for (id = 0; id < HWICAP_DEVICES; id++)
  525. if (!probed_devices[id])
  526. break;
  527. }
  528. if (id < 0 || id >= HWICAP_DEVICES) {
  529. dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
  530. return -EINVAL;
  531. }
  532. if (probed_devices[id]) {
  533. dev_err(dev, "cannot assign to %s%i; it is already in use\n",
  534. DRIVER_NAME, id);
  535. return -EBUSY;
  536. }
  537. probed_devices[id] = 1;
  538. devt = MKDEV(xhwicap_major, xhwicap_minor + id);
  539. drvdata = kmalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
  540. if (!drvdata) {
  541. dev_err(dev, "Couldn't allocate device private record\n");
  542. return -ENOMEM;
  543. }
  544. memset((void *)drvdata, 0, sizeof(struct hwicap_drvdata));
  545. dev_set_drvdata(dev, (void *)drvdata);
  546. if (!regs_res) {
  547. dev_err(dev, "Couldn't get registers resource\n");
  548. retval = -EFAULT;
  549. goto failed1;
  550. }
  551. drvdata->mem_start = regs_res->start;
  552. drvdata->mem_end = regs_res->end;
  553. drvdata->mem_size = regs_res->end - regs_res->start + 1;
  554. if (!request_mem_region(drvdata->mem_start,
  555. drvdata->mem_size, DRIVER_NAME)) {
  556. dev_err(dev, "Couldn't lock memory region at %p\n",
  557. (void *)regs_res->start);
  558. retval = -EBUSY;
  559. goto failed1;
  560. }
  561. drvdata->devt = devt;
  562. drvdata->dev = dev;
  563. drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
  564. if (!drvdata->base_address) {
  565. dev_err(dev, "ioremap() failed\n");
  566. goto failed2;
  567. }
  568. drvdata->config = config;
  569. drvdata->config_regs = config_regs;
  570. init_MUTEX(&drvdata->sem);
  571. drvdata->is_open = 0;
  572. dev_info(dev, "ioremap %lx to %p with size %x\n",
  573. (unsigned long int)drvdata->mem_start,
  574. drvdata->base_address, drvdata->mem_size);
  575. cdev_init(&drvdata->cdev, &hwicap_fops);
  576. drvdata->cdev.owner = THIS_MODULE;
  577. retval = cdev_add(&drvdata->cdev, devt, 1);
  578. if (retval) {
  579. dev_err(dev, "cdev_add() failed\n");
  580. goto failed3;
  581. }
  582. /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
  583. class_device_create(icap_class, NULL, devt, NULL, DRIVER_NAME);
  584. return 0; /* success */
  585. failed3:
  586. iounmap(drvdata->base_address);
  587. failed2:
  588. release_mem_region(regs_res->start, drvdata->mem_size);
  589. failed1:
  590. kfree(drvdata);
  591. return retval;
  592. }
  593. static struct hwicap_driver_config buffer_icap_config = {
  594. .get_configuration = buffer_icap_get_configuration,
  595. .set_configuration = buffer_icap_set_configuration,
  596. .reset = buffer_icap_reset,
  597. };
  598. static struct hwicap_driver_config fifo_icap_config = {
  599. .get_configuration = fifo_icap_get_configuration,
  600. .set_configuration = fifo_icap_set_configuration,
  601. .reset = fifo_icap_reset,
  602. };
  603. static int __devexit hwicap_remove(struct device *dev)
  604. {
  605. struct hwicap_drvdata *drvdata;
  606. drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
  607. if (!drvdata)
  608. return 0;
  609. class_device_destroy(icap_class, drvdata->devt);
  610. cdev_del(&drvdata->cdev);
  611. iounmap(drvdata->base_address);
  612. release_mem_region(drvdata->mem_start, drvdata->mem_size);
  613. kfree(drvdata);
  614. dev_set_drvdata(dev, NULL);
  615. probed_devices[MINOR(dev->devt)-xhwicap_minor] = 0;
  616. return 0; /* success */
  617. }
  618. static int __devinit hwicap_drv_probe(struct platform_device *pdev)
  619. {
  620. struct resource *res;
  621. const struct config_registers *regs;
  622. const char *family;
  623. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  624. if (!res)
  625. return -ENODEV;
  626. /* It's most likely that we're using V4, if the family is not
  627. specified */
  628. regs = &v4_config_registers;
  629. family = pdev->dev.platform_data;
  630. if (family) {
  631. if (!strcmp(family, "virtex2p")) {
  632. regs = &v2_config_registers;
  633. } else if (!strcmp(family, "virtex4")) {
  634. regs = &v4_config_registers;
  635. } else if (!strcmp(family, "virtex5")) {
  636. regs = &v5_config_registers;
  637. }
  638. }
  639. return hwicap_setup(&pdev->dev, pdev->id, res,
  640. &buffer_icap_config, regs);
  641. }
  642. static int __devexit hwicap_drv_remove(struct platform_device *pdev)
  643. {
  644. return hwicap_remove(&pdev->dev);
  645. }
  646. static struct platform_driver hwicap_platform_driver = {
  647. .probe = hwicap_drv_probe,
  648. .remove = hwicap_drv_remove,
  649. .driver = {
  650. .owner = THIS_MODULE,
  651. .name = DRIVER_NAME,
  652. },
  653. };
  654. /* ---------------------------------------------------------------------
  655. * OF bus binding
  656. */
  657. #if defined(CONFIG_OF)
  658. static int __devinit
  659. hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
  660. {
  661. struct resource res;
  662. const unsigned int *id;
  663. const char *family;
  664. int rc;
  665. const struct hwicap_driver_config *config = match->data;
  666. const struct config_registers *regs;
  667. dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
  668. rc = of_address_to_resource(op->node, 0, &res);
  669. if (rc) {
  670. dev_err(&op->dev, "invalid address\n");
  671. return rc;
  672. }
  673. id = of_get_property(op->node, "port-number", NULL);
  674. /* It's most likely that we're using V4, if the family is not
  675. specified */
  676. regs = &v4_config_registers;
  677. family = of_get_property(op->node, "xlnx,family", NULL);
  678. if (family) {
  679. if (!strcmp(family, "virtex2p")) {
  680. regs = &v2_config_registers;
  681. } else if (!strcmp(family, "virtex4")) {
  682. regs = &v4_config_registers;
  683. } else if (!strcmp(family, "virtex5")) {
  684. regs = &v5_config_registers;
  685. }
  686. }
  687. return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
  688. regs);
  689. }
  690. static int __devexit hwicap_of_remove(struct of_device *op)
  691. {
  692. return hwicap_remove(&op->dev);
  693. }
  694. /* Match table for of_platform binding */
  695. static const struct of_device_id __devinit hwicap_of_match[] = {
  696. { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
  697. { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
  698. {},
  699. };
  700. MODULE_DEVICE_TABLE(of, hwicap_of_match);
  701. static struct of_platform_driver hwicap_of_driver = {
  702. .owner = THIS_MODULE,
  703. .name = DRIVER_NAME,
  704. .match_table = hwicap_of_match,
  705. .probe = hwicap_of_probe,
  706. .remove = __devexit_p(hwicap_of_remove),
  707. .driver = {
  708. .name = DRIVER_NAME,
  709. },
  710. };
  711. /* Registration helpers to keep the number of #ifdefs to a minimum */
  712. static inline int __devinit hwicap_of_register(void)
  713. {
  714. pr_debug("hwicap: calling of_register_platform_driver()\n");
  715. return of_register_platform_driver(&hwicap_of_driver);
  716. }
  717. static inline void __devexit hwicap_of_unregister(void)
  718. {
  719. of_unregister_platform_driver(&hwicap_of_driver);
  720. }
  721. #else /* CONFIG_OF */
  722. /* CONFIG_OF not enabled; do nothing helpers */
  723. static inline int __devinit hwicap_of_register(void) { return 0; }
  724. static inline void __devexit hwicap_of_unregister(void) { }
  725. #endif /* CONFIG_OF */
  726. static int __devinit hwicap_module_init(void)
  727. {
  728. dev_t devt;
  729. int retval;
  730. icap_class = class_create(THIS_MODULE, "xilinx_config");
  731. if (xhwicap_major) {
  732. devt = MKDEV(xhwicap_major, xhwicap_minor);
  733. retval = register_chrdev_region(
  734. devt,
  735. HWICAP_DEVICES,
  736. DRIVER_NAME);
  737. if (retval < 0)
  738. return retval;
  739. } else {
  740. retval = alloc_chrdev_region(&devt,
  741. xhwicap_minor,
  742. HWICAP_DEVICES,
  743. DRIVER_NAME);
  744. if (retval < 0)
  745. return retval;
  746. xhwicap_major = MAJOR(devt);
  747. }
  748. retval = platform_driver_register(&hwicap_platform_driver);
  749. if (retval)
  750. goto failed1;
  751. retval = hwicap_of_register();
  752. if (retval)
  753. goto failed2;
  754. return retval;
  755. failed2:
  756. platform_driver_unregister(&hwicap_platform_driver);
  757. failed1:
  758. unregister_chrdev_region(devt, HWICAP_DEVICES);
  759. return retval;
  760. }
  761. static void __devexit hwicap_module_cleanup(void)
  762. {
  763. dev_t devt = MKDEV(xhwicap_major, xhwicap_minor);
  764. class_destroy(icap_class);
  765. platform_driver_unregister(&hwicap_platform_driver);
  766. hwicap_of_unregister();
  767. unregister_chrdev_region(devt, HWICAP_DEVICES);
  768. }
  769. module_init(hwicap_module_init);
  770. module_exit(hwicap_module_cleanup);
  771. MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
  772. MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
  773. MODULE_LICENSE("GPL");