xilinx_hwicap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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/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/icap0
  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/smp_lock.h>
  87. #include <linux/sysctl.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 "icap"
  103. #define HWICAP_REGS (0x10000)
  104. #define XHWICAP_MAJOR 259
  105. #define XHWICAP_MINOR 0
  106. #define HWICAP_DEVICES 1
  107. /* An array, which is set to true when the device is registered. */
  108. static bool probed_devices[HWICAP_DEVICES];
  109. static struct mutex icap_sem;
  110. static struct class *icap_class;
  111. #define UNIMPLEMENTED 0xFFFF
  112. static const struct config_registers v2_config_registers = {
  113. .CRC = 0,
  114. .FAR = 1,
  115. .FDRI = 2,
  116. .FDRO = 3,
  117. .CMD = 4,
  118. .CTL = 5,
  119. .MASK = 6,
  120. .STAT = 7,
  121. .LOUT = 8,
  122. .COR = 9,
  123. .MFWR = 10,
  124. .FLR = 11,
  125. .KEY = 12,
  126. .CBC = 13,
  127. .IDCODE = 14,
  128. .AXSS = UNIMPLEMENTED,
  129. .C0R_1 = UNIMPLEMENTED,
  130. .CSOB = UNIMPLEMENTED,
  131. .WBSTAR = UNIMPLEMENTED,
  132. .TIMER = UNIMPLEMENTED,
  133. .BOOTSTS = UNIMPLEMENTED,
  134. .CTL_1 = UNIMPLEMENTED,
  135. };
  136. static const struct config_registers v4_config_registers = {
  137. .CRC = 0,
  138. .FAR = 1,
  139. .FDRI = 2,
  140. .FDRO = 3,
  141. .CMD = 4,
  142. .CTL = 5,
  143. .MASK = 6,
  144. .STAT = 7,
  145. .LOUT = 8,
  146. .COR = 9,
  147. .MFWR = 10,
  148. .FLR = UNIMPLEMENTED,
  149. .KEY = UNIMPLEMENTED,
  150. .CBC = 11,
  151. .IDCODE = 12,
  152. .AXSS = 13,
  153. .C0R_1 = UNIMPLEMENTED,
  154. .CSOB = UNIMPLEMENTED,
  155. .WBSTAR = UNIMPLEMENTED,
  156. .TIMER = UNIMPLEMENTED,
  157. .BOOTSTS = UNIMPLEMENTED,
  158. .CTL_1 = UNIMPLEMENTED,
  159. };
  160. static const struct config_registers v5_config_registers = {
  161. .CRC = 0,
  162. .FAR = 1,
  163. .FDRI = 2,
  164. .FDRO = 3,
  165. .CMD = 4,
  166. .CTL = 5,
  167. .MASK = 6,
  168. .STAT = 7,
  169. .LOUT = 8,
  170. .COR = 9,
  171. .MFWR = 10,
  172. .FLR = UNIMPLEMENTED,
  173. .KEY = UNIMPLEMENTED,
  174. .CBC = 11,
  175. .IDCODE = 12,
  176. .AXSS = 13,
  177. .C0R_1 = 14,
  178. .CSOB = 15,
  179. .WBSTAR = 16,
  180. .TIMER = 17,
  181. .BOOTSTS = 18,
  182. .CTL_1 = 19,
  183. };
  184. /**
  185. * hwicap_command_desync - Send a DESYNC command to the ICAP port.
  186. * @drvdata: a pointer to the drvdata.
  187. *
  188. * This command desynchronizes the ICAP After this command, a
  189. * bitstream containing a NULL packet, followed by a SYNCH packet is
  190. * required before the ICAP will recognize commands.
  191. */
  192. static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
  193. {
  194. u32 buffer[4];
  195. u32 index = 0;
  196. /*
  197. * Create the data to be written to the ICAP.
  198. */
  199. buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
  200. buffer[index++] = XHI_CMD_DESYNCH;
  201. buffer[index++] = XHI_NOOP_PACKET;
  202. buffer[index++] = XHI_NOOP_PACKET;
  203. /*
  204. * Write the data to the FIFO and intiate the transfer of data present
  205. * in the FIFO to the ICAP device.
  206. */
  207. return drvdata->config->set_configuration(drvdata,
  208. &buffer[0], index);
  209. }
  210. /**
  211. * hwicap_get_configuration_register - Query a configuration register.
  212. * @drvdata: a pointer to the drvdata.
  213. * @reg: a constant which represents the configuration
  214. * register value to be returned.
  215. * Examples: XHI_IDCODE, XHI_FLR.
  216. * @reg_data: returns the value of the register.
  217. *
  218. * Sends a query packet to the ICAP and then receives the response.
  219. * The icap is left in Synched state.
  220. */
  221. static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
  222. u32 reg, u32 *reg_data)
  223. {
  224. int status;
  225. u32 buffer[6];
  226. u32 index = 0;
  227. /*
  228. * Create the data to be written to the ICAP.
  229. */
  230. buffer[index++] = XHI_DUMMY_PACKET;
  231. buffer[index++] = XHI_NOOP_PACKET;
  232. buffer[index++] = XHI_SYNC_PACKET;
  233. buffer[index++] = XHI_NOOP_PACKET;
  234. buffer[index++] = XHI_NOOP_PACKET;
  235. /*
  236. * Write the data to the FIFO and initiate the transfer of data present
  237. * in the FIFO to the ICAP device.
  238. */
  239. status = drvdata->config->set_configuration(drvdata,
  240. &buffer[0], index);
  241. if (status)
  242. return status;
  243. /* If the syncword was not found, then we need to start over. */
  244. status = drvdata->config->get_status(drvdata);
  245. if ((status & XHI_SR_DALIGN_MASK) != XHI_SR_DALIGN_MASK)
  246. return -EIO;
  247. index = 0;
  248. buffer[index++] = hwicap_type_1_read(reg) | 1;
  249. buffer[index++] = XHI_NOOP_PACKET;
  250. buffer[index++] = XHI_NOOP_PACKET;
  251. /*
  252. * Write the data to the FIFO and intiate the transfer of data present
  253. * in the FIFO to the ICAP device.
  254. */
  255. status = drvdata->config->set_configuration(drvdata,
  256. &buffer[0], index);
  257. if (status)
  258. return status;
  259. /*
  260. * Read the configuration register
  261. */
  262. status = drvdata->config->get_configuration(drvdata, reg_data, 1);
  263. if (status)
  264. return status;
  265. return 0;
  266. }
  267. static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
  268. {
  269. int status;
  270. u32 idcode;
  271. dev_dbg(drvdata->dev, "initializing\n");
  272. /* Abort any current transaction, to make sure we have the
  273. * ICAP in a good state. */
  274. dev_dbg(drvdata->dev, "Reset...\n");
  275. drvdata->config->reset(drvdata);
  276. dev_dbg(drvdata->dev, "Desync...\n");
  277. status = hwicap_command_desync(drvdata);
  278. if (status)
  279. return status;
  280. /* Attempt to read the IDCODE from ICAP. This
  281. * may not be returned correctly, due to the design of the
  282. * hardware.
  283. */
  284. dev_dbg(drvdata->dev, "Reading IDCODE...\n");
  285. status = hwicap_get_configuration_register(
  286. drvdata, drvdata->config_regs->IDCODE, &idcode);
  287. dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
  288. if (status)
  289. return status;
  290. dev_dbg(drvdata->dev, "Desync...\n");
  291. status = hwicap_command_desync(drvdata);
  292. if (status)
  293. return status;
  294. return 0;
  295. }
  296. static ssize_t
  297. hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  298. {
  299. struct hwicap_drvdata *drvdata = file->private_data;
  300. ssize_t bytes_to_read = 0;
  301. u32 *kbuf;
  302. u32 words;
  303. u32 bytes_remaining;
  304. int status;
  305. status = mutex_lock_interruptible(&drvdata->sem);
  306. if (status)
  307. return status;
  308. if (drvdata->read_buffer_in_use) {
  309. /* If there are leftover bytes in the buffer, just */
  310. /* return them and don't try to read more from the */
  311. /* ICAP device. */
  312. bytes_to_read =
  313. (count < drvdata->read_buffer_in_use) ? count :
  314. drvdata->read_buffer_in_use;
  315. /* Return the data currently in the read buffer. */
  316. if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
  317. status = -EFAULT;
  318. goto error;
  319. }
  320. drvdata->read_buffer_in_use -= bytes_to_read;
  321. memmove(drvdata->read_buffer,
  322. drvdata->read_buffer + bytes_to_read,
  323. 4 - bytes_to_read);
  324. } else {
  325. /* Get new data from the ICAP, and return was was requested. */
  326. kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
  327. if (!kbuf) {
  328. status = -ENOMEM;
  329. goto error;
  330. }
  331. /* The ICAP device is only able to read complete */
  332. /* words. If a number of bytes that do not correspond */
  333. /* to complete words is requested, then we read enough */
  334. /* words to get the required number of bytes, and then */
  335. /* save the remaining bytes for the next read. */
  336. /* Determine the number of words to read, rounding up */
  337. /* if necessary. */
  338. words = ((count + 3) >> 2);
  339. bytes_to_read = words << 2;
  340. if (bytes_to_read > PAGE_SIZE)
  341. bytes_to_read = PAGE_SIZE;
  342. /* Ensure we only read a complete number of words. */
  343. bytes_remaining = bytes_to_read & 3;
  344. bytes_to_read &= ~3;
  345. words = bytes_to_read >> 2;
  346. status = drvdata->config->get_configuration(drvdata,
  347. kbuf, words);
  348. /* If we didn't read correctly, then bail out. */
  349. if (status) {
  350. free_page((unsigned long)kbuf);
  351. goto error;
  352. }
  353. /* If we fail to return the data to the user, then bail out. */
  354. if (copy_to_user(buf, kbuf, bytes_to_read)) {
  355. free_page((unsigned long)kbuf);
  356. status = -EFAULT;
  357. goto error;
  358. }
  359. memcpy(drvdata->read_buffer,
  360. kbuf,
  361. bytes_remaining);
  362. drvdata->read_buffer_in_use = bytes_remaining;
  363. free_page((unsigned long)kbuf);
  364. }
  365. status = bytes_to_read;
  366. error:
  367. mutex_unlock(&drvdata->sem);
  368. return status;
  369. }
  370. static ssize_t
  371. hwicap_write(struct file *file, const char __user *buf,
  372. size_t count, loff_t *ppos)
  373. {
  374. struct hwicap_drvdata *drvdata = file->private_data;
  375. ssize_t written = 0;
  376. ssize_t left = count;
  377. u32 *kbuf;
  378. ssize_t len;
  379. ssize_t status;
  380. status = mutex_lock_interruptible(&drvdata->sem);
  381. if (status)
  382. return status;
  383. left += drvdata->write_buffer_in_use;
  384. /* Only write multiples of 4 bytes. */
  385. if (left < 4) {
  386. status = 0;
  387. goto error;
  388. }
  389. kbuf = (u32 *) __get_free_page(GFP_KERNEL);
  390. if (!kbuf) {
  391. status = -ENOMEM;
  392. goto error;
  393. }
  394. while (left > 3) {
  395. /* only write multiples of 4 bytes, so there might */
  396. /* be as many as 3 bytes left (at the end). */
  397. len = left;
  398. if (len > PAGE_SIZE)
  399. len = PAGE_SIZE;
  400. len &= ~3;
  401. if (drvdata->write_buffer_in_use) {
  402. memcpy(kbuf, drvdata->write_buffer,
  403. drvdata->write_buffer_in_use);
  404. if (copy_from_user(
  405. (((char *)kbuf) + drvdata->write_buffer_in_use),
  406. buf + written,
  407. len - (drvdata->write_buffer_in_use))) {
  408. free_page((unsigned long)kbuf);
  409. status = -EFAULT;
  410. goto error;
  411. }
  412. } else {
  413. if (copy_from_user(kbuf, buf + written, len)) {
  414. free_page((unsigned long)kbuf);
  415. status = -EFAULT;
  416. goto error;
  417. }
  418. }
  419. status = drvdata->config->set_configuration(drvdata,
  420. kbuf, len >> 2);
  421. if (status) {
  422. free_page((unsigned long)kbuf);
  423. status = -EFAULT;
  424. goto error;
  425. }
  426. if (drvdata->write_buffer_in_use) {
  427. len -= drvdata->write_buffer_in_use;
  428. left -= drvdata->write_buffer_in_use;
  429. drvdata->write_buffer_in_use = 0;
  430. }
  431. written += len;
  432. left -= len;
  433. }
  434. if ((left > 0) && (left < 4)) {
  435. if (!copy_from_user(drvdata->write_buffer,
  436. buf + written, left)) {
  437. drvdata->write_buffer_in_use = left;
  438. written += left;
  439. left = 0;
  440. }
  441. }
  442. free_page((unsigned long)kbuf);
  443. status = written;
  444. error:
  445. mutex_unlock(&drvdata->sem);
  446. return status;
  447. }
  448. static int hwicap_open(struct inode *inode, struct file *file)
  449. {
  450. struct hwicap_drvdata *drvdata;
  451. int status;
  452. lock_kernel();
  453. drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
  454. status = mutex_lock_interruptible(&drvdata->sem);
  455. if (status)
  456. goto out;
  457. if (drvdata->is_open) {
  458. status = -EBUSY;
  459. goto error;
  460. }
  461. status = hwicap_initialize_hwicap(drvdata);
  462. if (status) {
  463. dev_err(drvdata->dev, "Failed to open file");
  464. goto error;
  465. }
  466. file->private_data = drvdata;
  467. drvdata->write_buffer_in_use = 0;
  468. drvdata->read_buffer_in_use = 0;
  469. drvdata->is_open = 1;
  470. error:
  471. mutex_unlock(&drvdata->sem);
  472. out:
  473. unlock_kernel();
  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 %Lx\n",
  553. 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 %Lx\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. device_create_drvdata(icap_class, dev, devt, NULL,
  579. "%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 __devinitconst 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. devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
  737. retval = register_chrdev_region(devt,
  738. HWICAP_DEVICES,
  739. DRIVER_NAME);
  740. if (retval < 0)
  741. return retval;
  742. retval = platform_driver_register(&hwicap_platform_driver);
  743. if (retval)
  744. goto failed1;
  745. retval = hwicap_of_register();
  746. if (retval)
  747. goto failed2;
  748. return retval;
  749. failed2:
  750. platform_driver_unregister(&hwicap_platform_driver);
  751. failed1:
  752. unregister_chrdev_region(devt, HWICAP_DEVICES);
  753. return retval;
  754. }
  755. static void __exit hwicap_module_cleanup(void)
  756. {
  757. dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
  758. class_destroy(icap_class);
  759. platform_driver_unregister(&hwicap_platform_driver);
  760. hwicap_of_unregister();
  761. unregister_chrdev_region(devt, HWICAP_DEVICES);
  762. }
  763. module_init(hwicap_module_init);
  764. module_exit(hwicap_module_cleanup);
  765. MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
  766. MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
  767. MODULE_LICENSE("GPL");