xilinx_hwicap.c 22 KB

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