xilinx_hwicap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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/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 "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. drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
  453. status = mutex_lock_interruptible(&drvdata->sem);
  454. if (status)
  455. return status;
  456. if (drvdata->is_open) {
  457. status = -EBUSY;
  458. goto error;
  459. }
  460. status = hwicap_initialize_hwicap(drvdata);
  461. if (status) {
  462. dev_err(drvdata->dev, "Failed to open file");
  463. goto error;
  464. }
  465. file->private_data = drvdata;
  466. drvdata->write_buffer_in_use = 0;
  467. drvdata->read_buffer_in_use = 0;
  468. drvdata->is_open = 1;
  469. error:
  470. mutex_unlock(&drvdata->sem);
  471. return status;
  472. }
  473. static int hwicap_release(struct inode *inode, struct file *file)
  474. {
  475. struct hwicap_drvdata *drvdata = file->private_data;
  476. int i;
  477. int status = 0;
  478. mutex_lock(&drvdata->sem);
  479. if (drvdata->write_buffer_in_use) {
  480. /* Flush write buffer. */
  481. for (i = drvdata->write_buffer_in_use; i < 4; i++)
  482. drvdata->write_buffer[i] = 0;
  483. status = drvdata->config->set_configuration(drvdata,
  484. (u32 *) drvdata->write_buffer, 1);
  485. if (status)
  486. goto error;
  487. }
  488. status = hwicap_command_desync(drvdata);
  489. if (status)
  490. goto error;
  491. error:
  492. drvdata->is_open = 0;
  493. mutex_unlock(&drvdata->sem);
  494. return status;
  495. }
  496. static struct file_operations hwicap_fops = {
  497. .owner = THIS_MODULE,
  498. .write = hwicap_write,
  499. .read = hwicap_read,
  500. .open = hwicap_open,
  501. .release = hwicap_release,
  502. };
  503. static int __devinit hwicap_setup(struct device *dev, int id,
  504. const struct resource *regs_res,
  505. const struct hwicap_driver_config *config,
  506. const struct config_registers *config_regs)
  507. {
  508. dev_t devt;
  509. struct hwicap_drvdata *drvdata = NULL;
  510. int retval = 0;
  511. dev_info(dev, "Xilinx icap port driver\n");
  512. mutex_lock(&icap_sem);
  513. if (id < 0) {
  514. for (id = 0; id < HWICAP_DEVICES; id++)
  515. if (!probed_devices[id])
  516. break;
  517. }
  518. if (id < 0 || id >= HWICAP_DEVICES) {
  519. mutex_unlock(&icap_sem);
  520. dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
  521. return -EINVAL;
  522. }
  523. if (probed_devices[id]) {
  524. mutex_unlock(&icap_sem);
  525. dev_err(dev, "cannot assign to %s%i; it is already in use\n",
  526. DRIVER_NAME, id);
  527. return -EBUSY;
  528. }
  529. probed_devices[id] = 1;
  530. mutex_unlock(&icap_sem);
  531. devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR + id);
  532. drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
  533. if (!drvdata) {
  534. dev_err(dev, "Couldn't allocate device private record\n");
  535. retval = -ENOMEM;
  536. goto failed0;
  537. }
  538. dev_set_drvdata(dev, (void *)drvdata);
  539. if (!regs_res) {
  540. dev_err(dev, "Couldn't get registers resource\n");
  541. retval = -EFAULT;
  542. goto failed1;
  543. }
  544. drvdata->mem_start = regs_res->start;
  545. drvdata->mem_end = regs_res->end;
  546. drvdata->mem_size = regs_res->end - regs_res->start + 1;
  547. if (!request_mem_region(drvdata->mem_start,
  548. drvdata->mem_size, DRIVER_NAME)) {
  549. dev_err(dev, "Couldn't lock memory region at %p\n",
  550. (void *)regs_res->start);
  551. retval = -EBUSY;
  552. goto failed1;
  553. }
  554. drvdata->devt = devt;
  555. drvdata->dev = dev;
  556. drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
  557. if (!drvdata->base_address) {
  558. dev_err(dev, "ioremap() failed\n");
  559. goto failed2;
  560. }
  561. drvdata->config = config;
  562. drvdata->config_regs = config_regs;
  563. mutex_init(&drvdata->sem);
  564. drvdata->is_open = 0;
  565. dev_info(dev, "ioremap %lx to %p with size %x\n",
  566. (unsigned long int)drvdata->mem_start,
  567. drvdata->base_address, drvdata->mem_size);
  568. cdev_init(&drvdata->cdev, &hwicap_fops);
  569. drvdata->cdev.owner = THIS_MODULE;
  570. retval = cdev_add(&drvdata->cdev, devt, 1);
  571. if (retval) {
  572. dev_err(dev, "cdev_add() failed\n");
  573. goto failed3;
  574. }
  575. /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
  576. device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id);
  577. return 0; /* success */
  578. failed3:
  579. iounmap(drvdata->base_address);
  580. failed2:
  581. release_mem_region(regs_res->start, drvdata->mem_size);
  582. failed1:
  583. kfree(drvdata);
  584. failed0:
  585. mutex_lock(&icap_sem);
  586. probed_devices[id] = 0;
  587. mutex_unlock(&icap_sem);
  588. return retval;
  589. }
  590. static struct hwicap_driver_config buffer_icap_config = {
  591. .get_configuration = buffer_icap_get_configuration,
  592. .set_configuration = buffer_icap_set_configuration,
  593. .get_status = buffer_icap_get_status,
  594. .reset = buffer_icap_reset,
  595. };
  596. static struct hwicap_driver_config fifo_icap_config = {
  597. .get_configuration = fifo_icap_get_configuration,
  598. .set_configuration = fifo_icap_set_configuration,
  599. .get_status = fifo_icap_get_status,
  600. .reset = fifo_icap_reset,
  601. };
  602. static int __devexit hwicap_remove(struct device *dev)
  603. {
  604. struct hwicap_drvdata *drvdata;
  605. drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
  606. if (!drvdata)
  607. return 0;
  608. device_destroy(icap_class, drvdata->devt);
  609. cdev_del(&drvdata->cdev);
  610. iounmap(drvdata->base_address);
  611. release_mem_region(drvdata->mem_start, drvdata->mem_size);
  612. kfree(drvdata);
  613. dev_set_drvdata(dev, NULL);
  614. mutex_lock(&icap_sem);
  615. probed_devices[MINOR(dev->devt)-XHWICAP_MINOR] = 0;
  616. mutex_unlock(&icap_sem);
  617. return 0; /* success */
  618. }
  619. static int __devinit hwicap_drv_probe(struct platform_device *pdev)
  620. {
  621. struct resource *res;
  622. const struct config_registers *regs;
  623. const char *family;
  624. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  625. if (!res)
  626. return -ENODEV;
  627. /* It's most likely that we're using V4, if the family is not
  628. specified */
  629. regs = &v4_config_registers;
  630. family = pdev->dev.platform_data;
  631. if (family) {
  632. if (!strcmp(family, "virtex2p")) {
  633. regs = &v2_config_registers;
  634. } else if (!strcmp(family, "virtex4")) {
  635. regs = &v4_config_registers;
  636. } else if (!strcmp(family, "virtex5")) {
  637. regs = &v5_config_registers;
  638. }
  639. }
  640. return hwicap_setup(&pdev->dev, pdev->id, res,
  641. &buffer_icap_config, regs);
  642. }
  643. static int __devexit hwicap_drv_remove(struct platform_device *pdev)
  644. {
  645. return hwicap_remove(&pdev->dev);
  646. }
  647. static struct platform_driver hwicap_platform_driver = {
  648. .probe = hwicap_drv_probe,
  649. .remove = hwicap_drv_remove,
  650. .driver = {
  651. .owner = THIS_MODULE,
  652. .name = DRIVER_NAME,
  653. },
  654. };
  655. /* ---------------------------------------------------------------------
  656. * OF bus binding
  657. */
  658. #if defined(CONFIG_OF)
  659. static int __devinit
  660. hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
  661. {
  662. struct resource res;
  663. const unsigned int *id;
  664. const char *family;
  665. int rc;
  666. const struct hwicap_driver_config *config = match->data;
  667. const struct config_registers *regs;
  668. dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
  669. rc = of_address_to_resource(op->node, 0, &res);
  670. if (rc) {
  671. dev_err(&op->dev, "invalid address\n");
  672. return rc;
  673. }
  674. id = of_get_property(op->node, "port-number", NULL);
  675. /* It's most likely that we're using V4, if the family is not
  676. specified */
  677. regs = &v4_config_registers;
  678. family = of_get_property(op->node, "xlnx,family", NULL);
  679. if (family) {
  680. if (!strcmp(family, "virtex2p")) {
  681. regs = &v2_config_registers;
  682. } else if (!strcmp(family, "virtex4")) {
  683. regs = &v4_config_registers;
  684. } else if (!strcmp(family, "virtex5")) {
  685. regs = &v5_config_registers;
  686. }
  687. }
  688. return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
  689. regs);
  690. }
  691. static int __devexit hwicap_of_remove(struct of_device *op)
  692. {
  693. return hwicap_remove(&op->dev);
  694. }
  695. /* Match table for of_platform binding */
  696. static const struct of_device_id __devinitconst hwicap_of_match[] = {
  697. { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
  698. { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
  699. {},
  700. };
  701. MODULE_DEVICE_TABLE(of, hwicap_of_match);
  702. static struct of_platform_driver hwicap_of_driver = {
  703. .owner = THIS_MODULE,
  704. .name = DRIVER_NAME,
  705. .match_table = hwicap_of_match,
  706. .probe = hwicap_of_probe,
  707. .remove = __devexit_p(hwicap_of_remove),
  708. .driver = {
  709. .name = DRIVER_NAME,
  710. },
  711. };
  712. /* Registration helpers to keep the number of #ifdefs to a minimum */
  713. static inline int __init hwicap_of_register(void)
  714. {
  715. pr_debug("hwicap: calling of_register_platform_driver()\n");
  716. return of_register_platform_driver(&hwicap_of_driver);
  717. }
  718. static inline void __exit hwicap_of_unregister(void)
  719. {
  720. of_unregister_platform_driver(&hwicap_of_driver);
  721. }
  722. #else /* CONFIG_OF */
  723. /* CONFIG_OF not enabled; do nothing helpers */
  724. static inline int __init hwicap_of_register(void) { return 0; }
  725. static inline void __exit hwicap_of_unregister(void) { }
  726. #endif /* CONFIG_OF */
  727. static int __init hwicap_module_init(void)
  728. {
  729. dev_t devt;
  730. int retval;
  731. icap_class = class_create(THIS_MODULE, "xilinx_config");
  732. mutex_init(&icap_sem);
  733. devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
  734. retval = register_chrdev_region(devt,
  735. HWICAP_DEVICES,
  736. DRIVER_NAME);
  737. if (retval < 0)
  738. return retval;
  739. retval = platform_driver_register(&hwicap_platform_driver);
  740. if (retval)
  741. goto failed1;
  742. retval = hwicap_of_register();
  743. if (retval)
  744. goto failed2;
  745. return retval;
  746. failed2:
  747. platform_driver_unregister(&hwicap_platform_driver);
  748. failed1:
  749. unregister_chrdev_region(devt, HWICAP_DEVICES);
  750. return retval;
  751. }
  752. static void __exit hwicap_module_cleanup(void)
  753. {
  754. dev_t devt = MKDEV(XHWICAP_MAJOR, XHWICAP_MINOR);
  755. class_destroy(icap_class);
  756. platform_driver_unregister(&hwicap_platform_driver);
  757. hwicap_of_unregister();
  758. unregister_chrdev_region(devt, HWICAP_DEVICES);
  759. }
  760. module_init(hwicap_module_init);
  761. module_exit(hwicap_module_cleanup);
  762. MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
  763. MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
  764. MODULE_LICENSE("GPL");