xilinx_hwicap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 <linux/mutex.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 mutex icap_sem;
  113. static struct class *icap_class;
  114. #define UNIMPLEMENTED 0xFFFF
  115. static const struct config_registers v2_config_registers = {
  116. .CRC = 0,
  117. .FAR = 1,
  118. .FDRI = 2,
  119. .FDRO = 3,
  120. .CMD = 4,
  121. .CTL = 5,
  122. .MASK = 6,
  123. .STAT = 7,
  124. .LOUT = 8,
  125. .COR = 9,
  126. .MFWR = 10,
  127. .FLR = 11,
  128. .KEY = 12,
  129. .CBC = 13,
  130. .IDCODE = 14,
  131. .AXSS = UNIMPLEMENTED,
  132. .C0R_1 = UNIMPLEMENTED,
  133. .CSOB = UNIMPLEMENTED,
  134. .WBSTAR = UNIMPLEMENTED,
  135. .TIMER = UNIMPLEMENTED,
  136. .BOOTSTS = UNIMPLEMENTED,
  137. .CTL_1 = UNIMPLEMENTED,
  138. };
  139. static const struct config_registers v4_config_registers = {
  140. .CRC = 0,
  141. .FAR = 1,
  142. .FDRI = 2,
  143. .FDRO = 3,
  144. .CMD = 4,
  145. .CTL = 5,
  146. .MASK = 6,
  147. .STAT = 7,
  148. .LOUT = 8,
  149. .COR = 9,
  150. .MFWR = 10,
  151. .FLR = UNIMPLEMENTED,
  152. .KEY = UNIMPLEMENTED,
  153. .CBC = 11,
  154. .IDCODE = 12,
  155. .AXSS = 13,
  156. .C0R_1 = UNIMPLEMENTED,
  157. .CSOB = UNIMPLEMENTED,
  158. .WBSTAR = UNIMPLEMENTED,
  159. .TIMER = UNIMPLEMENTED,
  160. .BOOTSTS = UNIMPLEMENTED,
  161. .CTL_1 = UNIMPLEMENTED,
  162. };
  163. static const struct config_registers v5_config_registers = {
  164. .CRC = 0,
  165. .FAR = 1,
  166. .FDRI = 2,
  167. .FDRO = 3,
  168. .CMD = 4,
  169. .CTL = 5,
  170. .MASK = 6,
  171. .STAT = 7,
  172. .LOUT = 8,
  173. .COR = 9,
  174. .MFWR = 10,
  175. .FLR = UNIMPLEMENTED,
  176. .KEY = UNIMPLEMENTED,
  177. .CBC = 11,
  178. .IDCODE = 12,
  179. .AXSS = 13,
  180. .C0R_1 = 14,
  181. .CSOB = 15,
  182. .WBSTAR = 16,
  183. .TIMER = 17,
  184. .BOOTSTS = 18,
  185. .CTL_1 = 19,
  186. };
  187. /**
  188. * hwicap_command_desync - Send a DESYNC command to the ICAP port.
  189. * @drvdata: a pointer to the drvdata.
  190. *
  191. * This command desynchronizes the ICAP After this command, a
  192. * bitstream containing a NULL packet, followed by a SYNCH packet is
  193. * required before the ICAP will recognize commands.
  194. */
  195. static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
  196. {
  197. u32 buffer[4];
  198. u32 index = 0;
  199. /*
  200. * Create the data to be written to the ICAP.
  201. */
  202. buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
  203. buffer[index++] = XHI_CMD_DESYNCH;
  204. buffer[index++] = XHI_NOOP_PACKET;
  205. buffer[index++] = XHI_NOOP_PACKET;
  206. /*
  207. * Write the data to the FIFO and intiate the transfer of data present
  208. * in the FIFO to the ICAP device.
  209. */
  210. return drvdata->config->set_configuration(drvdata,
  211. &buffer[0], index);
  212. }
  213. /**
  214. * hwicap_get_configuration_register - Query a configuration register.
  215. * @drvdata: a pointer to the drvdata.
  216. * @reg: a constant which represents the configuration
  217. * register value to be returned.
  218. * Examples: XHI_IDCODE, XHI_FLR.
  219. * @reg_data: returns the value of the register.
  220. *
  221. * Sends a query packet to the ICAP and then receives the response.
  222. * The icap is left in Synched state.
  223. */
  224. static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
  225. u32 reg, u32 *reg_data)
  226. {
  227. int status;
  228. u32 buffer[6];
  229. u32 index = 0;
  230. /*
  231. * Create the data to be written to the ICAP.
  232. */
  233. buffer[index++] = XHI_DUMMY_PACKET;
  234. buffer[index++] = XHI_NOOP_PACKET;
  235. buffer[index++] = XHI_SYNC_PACKET;
  236. buffer[index++] = XHI_NOOP_PACKET;
  237. buffer[index++] = XHI_NOOP_PACKET;
  238. /*
  239. * Write the data to the FIFO and initiate the transfer of data present
  240. * in the FIFO to the ICAP device.
  241. */
  242. status = drvdata->config->set_configuration(drvdata,
  243. &buffer[0], index);
  244. if (status)
  245. return status;
  246. /* If the syncword was not found, then we need to start over. */
  247. status = drvdata->config->get_status(drvdata);
  248. if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)
  249. return -EIO;
  250. index = 0;
  251. buffer[index++] = hwicap_type_1_read(reg) | 1;
  252. buffer[index++] = XHI_NOOP_PACKET;
  253. buffer[index++] = XHI_NOOP_PACKET;
  254. /*
  255. * Write the data to the FIFO and intiate the transfer of data present
  256. * in the FIFO to the ICAP device.
  257. */
  258. status = drvdata->config->set_configuration(drvdata,
  259. &buffer[0], index);
  260. if (status)
  261. return status;
  262. /*
  263. * Read the configuration register
  264. */
  265. status = drvdata->config->get_configuration(drvdata, reg_data, 1);
  266. if (status)
  267. return status;
  268. return 0;
  269. }
  270. static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
  271. {
  272. int status;
  273. u32 idcode;
  274. dev_dbg(drvdata->dev, "initializing\n");
  275. /* Abort any current transaction, to make sure we have the
  276. * ICAP in a good state. */
  277. dev_dbg(drvdata->dev, "Reset...\n");
  278. drvdata->config->reset(drvdata);
  279. dev_dbg(drvdata->dev, "Desync...\n");
  280. status = hwicap_command_desync(drvdata);
  281. if (status)
  282. return status;
  283. /* Attempt to read the IDCODE from ICAP. This
  284. * may not be returned correctly, due to the design of the
  285. * hardware.
  286. */
  287. dev_dbg(drvdata->dev, "Reading IDCODE...\n");
  288. status = hwicap_get_configuration_register(
  289. drvdata, drvdata->config_regs->IDCODE, &idcode);
  290. dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
  291. if (status)
  292. return status;
  293. dev_dbg(drvdata->dev, "Desync...\n");
  294. status = hwicap_command_desync(drvdata);
  295. if (status)
  296. return status;
  297. return 0;
  298. }
  299. static ssize_t
  300. hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  301. {
  302. struct hwicap_drvdata *drvdata = file->private_data;
  303. ssize_t bytes_to_read = 0;
  304. u32 *kbuf;
  305. u32 words;
  306. u32 bytes_remaining;
  307. int status;
  308. status = mutex_lock_interruptible(&drvdata->sem);
  309. if (status)
  310. return status;
  311. if (drvdata->read_buffer_in_use) {
  312. /* If there are leftover bytes in the buffer, just */
  313. /* return them and don't try to read more from the */
  314. /* ICAP device. */
  315. bytes_to_read =
  316. (count < drvdata->read_buffer_in_use) ? count :
  317. drvdata->read_buffer_in_use;
  318. /* Return the data currently in the read buffer. */
  319. if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
  320. status = -EFAULT;
  321. goto error;
  322. }
  323. drvdata->read_buffer_in_use -= bytes_to_read;
  324. memmove(drvdata->read_buffer,
  325. drvdata->read_buffer + bytes_to_read,
  326. 4 - bytes_to_read);
  327. } else {
  328. /* Get new data from the ICAP, and return was was requested. */
  329. kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
  330. if (!kbuf) {
  331. status = -ENOMEM;
  332. goto error;
  333. }
  334. /* The ICAP device is only able to read complete */
  335. /* words. If a number of bytes that do not correspond */
  336. /* to complete words is requested, then we read enough */
  337. /* words to get the required number of bytes, and then */
  338. /* save the remaining bytes for the next read. */
  339. /* Determine the number of words to read, rounding up */
  340. /* if necessary. */
  341. words = ((count + 3) >> 2);
  342. bytes_to_read = words << 2;
  343. if (bytes_to_read > PAGE_SIZE)
  344. bytes_to_read = PAGE_SIZE;
  345. /* Ensure we only read a complete number of words. */
  346. bytes_remaining = bytes_to_read & 3;
  347. bytes_to_read &= ~3;
  348. words = bytes_to_read >> 2;
  349. status = drvdata->config->get_configuration(drvdata,
  350. kbuf, words);
  351. /* If we didn't read correctly, then bail out. */
  352. if (status) {
  353. free_page((unsigned long)kbuf);
  354. goto error;
  355. }
  356. /* If we fail to return the data to the user, then bail out. */
  357. if (copy_to_user(buf, kbuf, bytes_to_read)) {
  358. free_page((unsigned long)kbuf);
  359. status = -EFAULT;
  360. goto error;
  361. }
  362. memcpy(drvdata->read_buffer,
  363. kbuf,
  364. bytes_remaining);
  365. drvdata->read_buffer_in_use = bytes_remaining;
  366. free_page((unsigned long)kbuf);
  367. }
  368. status = bytes_to_read;
  369. error:
  370. mutex_unlock(&drvdata->sem);
  371. return status;
  372. }
  373. static ssize_t
  374. hwicap_write(struct file *file, const char __user *buf,
  375. size_t count, loff_t *ppos)
  376. {
  377. struct hwicap_drvdata *drvdata = file->private_data;
  378. ssize_t written = 0;
  379. ssize_t left = count;
  380. u32 *kbuf;
  381. ssize_t len;
  382. ssize_t status;
  383. status = mutex_lock_interruptible(&drvdata->sem);
  384. if (status)
  385. return status;
  386. left += drvdata->write_buffer_in_use;
  387. /* Only write multiples of 4 bytes. */
  388. if (left < 4) {
  389. status = 0;
  390. goto error;
  391. }
  392. kbuf = (u32 *) __get_free_page(GFP_KERNEL);
  393. if (!kbuf) {
  394. status = -ENOMEM;
  395. goto error;
  396. }
  397. while (left > 3) {
  398. /* only write multiples of 4 bytes, so there might */
  399. /* be as many as 3 bytes left (at the end). */
  400. len = left;
  401. if (len > PAGE_SIZE)
  402. len = PAGE_SIZE;
  403. len &= ~3;
  404. if (drvdata->write_buffer_in_use) {
  405. memcpy(kbuf, drvdata->write_buffer,
  406. drvdata->write_buffer_in_use);
  407. if (copy_from_user(
  408. (((char *)kbuf) + drvdata->write_buffer_in_use),
  409. buf + written,
  410. len - (drvdata->write_buffer_in_use))) {
  411. free_page((unsigned long)kbuf);
  412. status = -EFAULT;
  413. goto error;
  414. }
  415. } else {
  416. if (copy_from_user(kbuf, buf + written, len)) {
  417. free_page((unsigned long)kbuf);
  418. status = -EFAULT;
  419. goto error;
  420. }
  421. }
  422. status = drvdata->config->set_configuration(drvdata,
  423. kbuf, len >> 2);
  424. if (status) {
  425. free_page((unsigned long)kbuf);
  426. status = -EFAULT;
  427. goto error;
  428. }
  429. if (drvdata->write_buffer_in_use) {
  430. len -= drvdata->write_buffer_in_use;
  431. left -= drvdata->write_buffer_in_use;
  432. drvdata->write_buffer_in_use = 0;
  433. }
  434. written += len;
  435. left -= len;
  436. }
  437. if ((left > 0) && (left < 4)) {
  438. if (!copy_from_user(drvdata->write_buffer,
  439. buf + written, left)) {
  440. drvdata->write_buffer_in_use = left;
  441. written += left;
  442. left = 0;
  443. }
  444. }
  445. free_page((unsigned long)kbuf);
  446. status = written;
  447. error:
  448. mutex_unlock(&drvdata->sem);
  449. return status;
  450. }
  451. static int hwicap_open(struct inode *inode, struct file *file)
  452. {
  453. struct hwicap_drvdata *drvdata;
  454. int status;
  455. drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
  456. status = mutex_lock_interruptible(&drvdata->sem);
  457. if (status)
  458. return status;
  459. if (drvdata->is_open) {
  460. status = -EBUSY;
  461. goto error;
  462. }
  463. status = hwicap_initialize_hwicap(drvdata);
  464. if (status) {
  465. dev_err(drvdata->dev, "Failed to open file");
  466. goto error;
  467. }
  468. file->private_data = drvdata;
  469. drvdata->write_buffer_in_use = 0;
  470. drvdata->read_buffer_in_use = 0;
  471. drvdata->is_open = 1;
  472. error:
  473. mutex_unlock(&drvdata->sem);
  474. return status;
  475. }
  476. static int hwicap_release(struct inode *inode, struct file *file)
  477. {
  478. struct hwicap_drvdata *drvdata = file->private_data;
  479. int i;
  480. int status = 0;
  481. mutex_lock(&drvdata->sem);
  482. if (drvdata->write_buffer_in_use) {
  483. /* Flush write buffer. */
  484. for (i = drvdata->write_buffer_in_use; i < 4; i++)
  485. drvdata->write_buffer[i] = 0;
  486. status = drvdata->config->set_configuration(drvdata,
  487. (u32 *) drvdata->write_buffer, 1);
  488. if (status)
  489. goto error;
  490. }
  491. status = hwicap_command_desync(drvdata);
  492. if (status)
  493. goto error;
  494. error:
  495. drvdata->is_open = 0;
  496. mutex_unlock(&drvdata->sem);
  497. return status;
  498. }
  499. static struct file_operations hwicap_fops = {
  500. .owner = THIS_MODULE,
  501. .write = hwicap_write,
  502. .read = hwicap_read,
  503. .open = hwicap_open,
  504. .release = hwicap_release,
  505. };
  506. static int __devinit hwicap_setup(struct device *dev, int id,
  507. const struct resource *regs_res,
  508. const struct hwicap_driver_config *config,
  509. const struct config_registers *config_regs)
  510. {
  511. dev_t devt;
  512. struct hwicap_drvdata *drvdata = NULL;
  513. int retval = 0;
  514. dev_info(dev, "Xilinx icap port driver\n");
  515. mutex_lock(&icap_sem);
  516. if (id < 0) {
  517. for (id = 0; id < HWICAP_DEVICES; id++)
  518. if (!probed_devices[id])
  519. break;
  520. }
  521. if (id < 0 || id >= HWICAP_DEVICES) {
  522. mutex_unlock(&icap_sem);
  523. dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
  524. return -EINVAL;
  525. }
  526. if (probed_devices[id]) {
  527. mutex_unlock(&icap_sem);
  528. dev_err(dev, "cannot assign to %s%i; it is already in use\n",
  529. DRIVER_NAME, id);
  530. return -EBUSY;
  531. }
  532. probed_devices[id] = 1;
  533. mutex_unlock(&icap_sem);
  534. devt = MKDEV(xhwicap_major, xhwicap_minor + id);
  535. drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
  536. if (!drvdata) {
  537. dev_err(dev, "Couldn't allocate device private record\n");
  538. retval = -ENOMEM;
  539. goto failed0;
  540. }
  541. dev_set_drvdata(dev, (void *)drvdata);
  542. if (!regs_res) {
  543. dev_err(dev, "Couldn't get registers resource\n");
  544. retval = -EFAULT;
  545. goto failed1;
  546. }
  547. drvdata->mem_start = regs_res->start;
  548. drvdata->mem_end = regs_res->end;
  549. drvdata->mem_size = regs_res->end - regs_res->start + 1;
  550. if (!request_mem_region(drvdata->mem_start,
  551. drvdata->mem_size, DRIVER_NAME)) {
  552. dev_err(dev, "Couldn't lock memory region at %p\n",
  553. (void *)regs_res->start);
  554. retval = -EBUSY;
  555. goto failed1;
  556. }
  557. drvdata->devt = devt;
  558. drvdata->dev = dev;
  559. drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
  560. if (!drvdata->base_address) {
  561. dev_err(dev, "ioremap() failed\n");
  562. goto failed2;
  563. }
  564. drvdata->config = config;
  565. drvdata->config_regs = config_regs;
  566. mutex_init(&drvdata->sem);
  567. drvdata->is_open = 0;
  568. dev_info(dev, "ioremap %lx to %p with size %x\n",
  569. (unsigned long int)drvdata->mem_start,
  570. drvdata->base_address, drvdata->mem_size);
  571. cdev_init(&drvdata->cdev, &hwicap_fops);
  572. drvdata->cdev.owner = THIS_MODULE;
  573. retval = cdev_add(&drvdata->cdev, devt, 1);
  574. if (retval) {
  575. dev_err(dev, "cdev_add() failed\n");
  576. goto failed3;
  577. }
  578. /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
  579. device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id);
  580. return 0; /* success */
  581. failed3:
  582. iounmap(drvdata->base_address);
  583. failed2:
  584. release_mem_region(regs_res->start, drvdata->mem_size);
  585. failed1:
  586. kfree(drvdata);
  587. failed0:
  588. mutex_lock(&icap_sem);
  589. probed_devices[id] = 0;
  590. mutex_unlock(&icap_sem);
  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. .get_status = buffer_icap_get_status,
  597. .reset = buffer_icap_reset,
  598. };
  599. static struct hwicap_driver_config fifo_icap_config = {
  600. .get_configuration = fifo_icap_get_configuration,
  601. .set_configuration = fifo_icap_set_configuration,
  602. .get_status = fifo_icap_get_status,
  603. .reset = fifo_icap_reset,
  604. };
  605. static int __devexit hwicap_remove(struct device *dev)
  606. {
  607. struct hwicap_drvdata *drvdata;
  608. drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
  609. if (!drvdata)
  610. return 0;
  611. device_destroy(icap_class, drvdata->devt);
  612. cdev_del(&drvdata->cdev);
  613. iounmap(drvdata->base_address);
  614. release_mem_region(drvdata->mem_start, drvdata->mem_size);
  615. kfree(drvdata);
  616. dev_set_drvdata(dev, NULL);
  617. mutex_lock(&icap_sem);
  618. probed_devices[MINOR(dev->devt)-xhwicap_minor] = 0;
  619. mutex_unlock(&icap_sem);
  620. return 0; /* success */
  621. }
  622. static int __devinit hwicap_drv_probe(struct platform_device *pdev)
  623. {
  624. struct resource *res;
  625. const struct config_registers *regs;
  626. const char *family;
  627. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  628. if (!res)
  629. return -ENODEV;
  630. /* It's most likely that we're using V4, if the family is not
  631. specified */
  632. regs = &v4_config_registers;
  633. family = pdev->dev.platform_data;
  634. if (family) {
  635. if (!strcmp(family, "virtex2p")) {
  636. regs = &v2_config_registers;
  637. } else if (!strcmp(family, "virtex4")) {
  638. regs = &v4_config_registers;
  639. } else if (!strcmp(family, "virtex5")) {
  640. regs = &v5_config_registers;
  641. }
  642. }
  643. return hwicap_setup(&pdev->dev, pdev->id, res,
  644. &buffer_icap_config, regs);
  645. }
  646. static int __devexit hwicap_drv_remove(struct platform_device *pdev)
  647. {
  648. return hwicap_remove(&pdev->dev);
  649. }
  650. static struct platform_driver hwicap_platform_driver = {
  651. .probe = hwicap_drv_probe,
  652. .remove = hwicap_drv_remove,
  653. .driver = {
  654. .owner = THIS_MODULE,
  655. .name = DRIVER_NAME,
  656. },
  657. };
  658. /* ---------------------------------------------------------------------
  659. * OF bus binding
  660. */
  661. #if defined(CONFIG_OF)
  662. static int __devinit
  663. hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
  664. {
  665. struct resource res;
  666. const unsigned int *id;
  667. const char *family;
  668. int rc;
  669. const struct hwicap_driver_config *config = match->data;
  670. const struct config_registers *regs;
  671. dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
  672. rc = of_address_to_resource(op->node, 0, &res);
  673. if (rc) {
  674. dev_err(&op->dev, "invalid address\n");
  675. return rc;
  676. }
  677. id = of_get_property(op->node, "port-number", NULL);
  678. /* It's most likely that we're using V4, if the family is not
  679. specified */
  680. regs = &v4_config_registers;
  681. family = of_get_property(op->node, "xlnx,family", NULL);
  682. if (family) {
  683. if (!strcmp(family, "virtex2p")) {
  684. regs = &v2_config_registers;
  685. } else if (!strcmp(family, "virtex4")) {
  686. regs = &v4_config_registers;
  687. } else if (!strcmp(family, "virtex5")) {
  688. regs = &v5_config_registers;
  689. }
  690. }
  691. return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
  692. regs);
  693. }
  694. static int __devexit hwicap_of_remove(struct of_device *op)
  695. {
  696. return hwicap_remove(&op->dev);
  697. }
  698. /* Match table for of_platform binding */
  699. static const struct of_device_id __devinit hwicap_of_match[] = {
  700. { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
  701. { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
  702. {},
  703. };
  704. MODULE_DEVICE_TABLE(of, hwicap_of_match);
  705. static struct of_platform_driver hwicap_of_driver = {
  706. .owner = THIS_MODULE,
  707. .name = DRIVER_NAME,
  708. .match_table = hwicap_of_match,
  709. .probe = hwicap_of_probe,
  710. .remove = __devexit_p(hwicap_of_remove),
  711. .driver = {
  712. .name = DRIVER_NAME,
  713. },
  714. };
  715. /* Registration helpers to keep the number of #ifdefs to a minimum */
  716. static inline int __init hwicap_of_register(void)
  717. {
  718. pr_debug("hwicap: calling of_register_platform_driver()\n");
  719. return of_register_platform_driver(&hwicap_of_driver);
  720. }
  721. static inline void __exit hwicap_of_unregister(void)
  722. {
  723. of_unregister_platform_driver(&hwicap_of_driver);
  724. }
  725. #else /* CONFIG_OF */
  726. /* CONFIG_OF not enabled; do nothing helpers */
  727. static inline int __init hwicap_of_register(void) { return 0; }
  728. static inline void __exit hwicap_of_unregister(void) { }
  729. #endif /* CONFIG_OF */
  730. static int __init hwicap_module_init(void)
  731. {
  732. dev_t devt;
  733. int retval;
  734. icap_class = class_create(THIS_MODULE, "xilinx_config");
  735. mutex_init(&icap_sem);
  736. if (xhwicap_major) {
  737. devt = MKDEV(xhwicap_major, xhwicap_minor);
  738. retval = register_chrdev_region(
  739. devt,
  740. HWICAP_DEVICES,
  741. DRIVER_NAME);
  742. if (retval < 0)
  743. return retval;
  744. } else {
  745. retval = alloc_chrdev_region(&devt,
  746. xhwicap_minor,
  747. HWICAP_DEVICES,
  748. DRIVER_NAME);
  749. if (retval < 0)
  750. return retval;
  751. xhwicap_major = MAJOR(devt);
  752. }
  753. retval = platform_driver_register(&hwicap_platform_driver);
  754. if (retval)
  755. goto failed1;
  756. retval = hwicap_of_register();
  757. if (retval)
  758. goto failed2;
  759. return retval;
  760. failed2:
  761. platform_driver_unregister(&hwicap_platform_driver);
  762. failed1:
  763. unregister_chrdev_region(devt, HWICAP_DEVICES);
  764. return retval;
  765. }
  766. static void __exit hwicap_module_cleanup(void)
  767. {
  768. dev_t devt = MKDEV(xhwicap_major, xhwicap_minor);
  769. class_destroy(icap_class);
  770. platform_driver_unregister(&hwicap_platform_driver);
  771. hwicap_of_unregister();
  772. unregister_chrdev_region(devt, HWICAP_DEVICES);
  773. }
  774. module_init(hwicap_module_init);
  775. module_exit(hwicap_module_cleanup);
  776. MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
  777. MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
  778. MODULE_LICENSE("GPL");