xsysace.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Xilinx SystemACE device driver
  3. *
  4. * Copyright 2007 Secret Lab Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. /*
  11. * The SystemACE chip is designed to configure FPGAs by loading an FPGA
  12. * bitstream from a file on a CF card and squirting it into FPGAs connected
  13. * to the SystemACE JTAG chain. It also has the advantage of providing an
  14. * MPU interface which can be used to control the FPGA configuration process
  15. * and to use the attached CF card for general purpose storage.
  16. *
  17. * This driver is a block device driver for the SystemACE.
  18. *
  19. * Initialization:
  20. * The driver registers itself as a platform_device driver at module
  21. * load time. The platform bus will take care of calling the
  22. * ace_probe() method for all SystemACE instances in the system. Any
  23. * number of SystemACE instances are supported. ace_probe() calls
  24. * ace_setup() which initialized all data structures, reads the CF
  25. * id structure and registers the device.
  26. *
  27. * Processing:
  28. * Just about all of the heavy lifting in this driver is performed by
  29. * a Finite State Machine (FSM). The driver needs to wait on a number
  30. * of events; some raised by interrupts, some which need to be polled
  31. * for. Describing all of the behaviour in a FSM seems to be the
  32. * easiest way to keep the complexity low and make it easy to
  33. * understand what the driver is doing. If the block ops or the
  34. * request function need to interact with the hardware, then they
  35. * simply need to flag the request and kick of FSM processing.
  36. *
  37. * The FSM itself is atomic-safe code which can be run from any
  38. * context. The general process flow is:
  39. * 1. obtain the ace->lock spinlock.
  40. * 2. loop on ace_fsm_dostate() until the ace->fsm_continue flag is
  41. * cleared.
  42. * 3. release the lock.
  43. *
  44. * Individual states do not sleep in any way. If a condition needs to
  45. * be waited for then the state much clear the fsm_continue flag and
  46. * either schedule the FSM to be run again at a later time, or expect
  47. * an interrupt to call the FSM when the desired condition is met.
  48. *
  49. * In normal operation, the FSM is processed at interrupt context
  50. * either when the driver's tasklet is scheduled, or when an irq is
  51. * raised by the hardware. The tasklet can be scheduled at any time.
  52. * The request method in particular schedules the tasklet when a new
  53. * request has been indicated by the block layer. Once started, the
  54. * FSM proceeds as far as it can processing the request until it
  55. * needs on a hardware event. At this point, it must yield execution.
  56. *
  57. * A state has two options when yielding execution:
  58. * 1. ace_fsm_yield()
  59. * - Call if need to poll for event.
  60. * - clears the fsm_continue flag to exit the processing loop
  61. * - reschedules the tasklet to run again as soon as possible
  62. * 2. ace_fsm_yieldirq()
  63. * - Call if an irq is expected from the HW
  64. * - clears the fsm_continue flag to exit the processing loop
  65. * - does not reschedule the tasklet so the FSM will not be processed
  66. * again until an irq is received.
  67. * After calling a yield function, the state must return control back
  68. * to the FSM main loop.
  69. *
  70. * Additionally, the driver maintains a kernel timer which can process
  71. * the FSM. If the FSM gets stalled, typically due to a missed
  72. * interrupt, then the kernel timer will expire and the driver can
  73. * continue where it left off.
  74. *
  75. * To Do:
  76. * - Add FPGA configuration control interface.
  77. * - Request major number from lanana
  78. */
  79. #undef DEBUG
  80. #include <linux/module.h>
  81. #include <linux/ctype.h>
  82. #include <linux/init.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/errno.h>
  85. #include <linux/kernel.h>
  86. #include <linux/delay.h>
  87. #include <linux/slab.h>
  88. #include <linux/blk-mq.h>
  89. #include <linux/mutex.h>
  90. #include <linux/ata.h>
  91. #include <linux/hdreg.h>
  92. #include <linux/platform_device.h>
  93. #if defined(CONFIG_OF)
  94. #include <linux/of_address.h>
  95. #include <linux/of_device.h>
  96. #include <linux/of_platform.h>
  97. #endif
  98. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  99. MODULE_DESCRIPTION("Xilinx SystemACE device driver");
  100. MODULE_LICENSE("GPL");
  101. /* SystemACE register definitions */
  102. #define ACE_BUSMODE (0x00)
  103. #define ACE_STATUS (0x04)
  104. #define ACE_STATUS_CFGLOCK (0x00000001)
  105. #define ACE_STATUS_MPULOCK (0x00000002)
  106. #define ACE_STATUS_CFGERROR (0x00000004) /* config controller error */
  107. #define ACE_STATUS_CFCERROR (0x00000008) /* CF controller error */
  108. #define ACE_STATUS_CFDETECT (0x00000010)
  109. #define ACE_STATUS_DATABUFRDY (0x00000020)
  110. #define ACE_STATUS_DATABUFMODE (0x00000040)
  111. #define ACE_STATUS_CFGDONE (0x00000080)
  112. #define ACE_STATUS_RDYFORCFCMD (0x00000100)
  113. #define ACE_STATUS_CFGMODEPIN (0x00000200)
  114. #define ACE_STATUS_CFGADDR_MASK (0x0000e000)
  115. #define ACE_STATUS_CFBSY (0x00020000)
  116. #define ACE_STATUS_CFRDY (0x00040000)
  117. #define ACE_STATUS_CFDWF (0x00080000)
  118. #define ACE_STATUS_CFDSC (0x00100000)
  119. #define ACE_STATUS_CFDRQ (0x00200000)
  120. #define ACE_STATUS_CFCORR (0x00400000)
  121. #define ACE_STATUS_CFERR (0x00800000)
  122. #define ACE_ERROR (0x08)
  123. #define ACE_CFGLBA (0x0c)
  124. #define ACE_MPULBA (0x10)
  125. #define ACE_SECCNTCMD (0x14)
  126. #define ACE_SECCNTCMD_RESET (0x0100)
  127. #define ACE_SECCNTCMD_IDENTIFY (0x0200)
  128. #define ACE_SECCNTCMD_READ_DATA (0x0300)
  129. #define ACE_SECCNTCMD_WRITE_DATA (0x0400)
  130. #define ACE_SECCNTCMD_ABORT (0x0600)
  131. #define ACE_VERSION (0x16)
  132. #define ACE_VERSION_REVISION_MASK (0x00FF)
  133. #define ACE_VERSION_MINOR_MASK (0x0F00)
  134. #define ACE_VERSION_MAJOR_MASK (0xF000)
  135. #define ACE_CTRL (0x18)
  136. #define ACE_CTRL_FORCELOCKREQ (0x0001)
  137. #define ACE_CTRL_LOCKREQ (0x0002)
  138. #define ACE_CTRL_FORCECFGADDR (0x0004)
  139. #define ACE_CTRL_FORCECFGMODE (0x0008)
  140. #define ACE_CTRL_CFGMODE (0x0010)
  141. #define ACE_CTRL_CFGSTART (0x0020)
  142. #define ACE_CTRL_CFGSEL (0x0040)
  143. #define ACE_CTRL_CFGRESET (0x0080)
  144. #define ACE_CTRL_DATABUFRDYIRQ (0x0100)
  145. #define ACE_CTRL_ERRORIRQ (0x0200)
  146. #define ACE_CTRL_CFGDONEIRQ (0x0400)
  147. #define ACE_CTRL_RESETIRQ (0x0800)
  148. #define ACE_CTRL_CFGPROG (0x1000)
  149. #define ACE_CTRL_CFGADDR_MASK (0xe000)
  150. #define ACE_FATSTAT (0x1c)
  151. #define ACE_NUM_MINORS 16
  152. #define ACE_SECTOR_SIZE (512)
  153. #define ACE_FIFO_SIZE (32)
  154. #define ACE_BUF_PER_SECTOR (ACE_SECTOR_SIZE / ACE_FIFO_SIZE)
  155. #define ACE_BUS_WIDTH_8 0
  156. #define ACE_BUS_WIDTH_16 1
  157. struct ace_reg_ops;
  158. struct ace_device {
  159. /* driver state data */
  160. int id;
  161. int media_change;
  162. int users;
  163. struct list_head list;
  164. /* finite state machine data */
  165. struct tasklet_struct fsm_tasklet;
  166. uint fsm_task; /* Current activity (ACE_TASK_*) */
  167. uint fsm_state; /* Current state (ACE_FSM_STATE_*) */
  168. uint fsm_continue_flag; /* cleared to exit FSM mainloop */
  169. uint fsm_iter_num;
  170. struct timer_list stall_timer;
  171. /* Transfer state/result, use for both id and block request */
  172. struct request *req; /* request being processed */
  173. void *data_ptr; /* pointer to I/O buffer */
  174. int data_count; /* number of buffers remaining */
  175. int data_result; /* Result of transfer; 0 := success */
  176. int id_req_count; /* count of id requests */
  177. int id_result;
  178. struct completion id_completion; /* used when id req finishes */
  179. int in_irq;
  180. /* Details of hardware device */
  181. resource_size_t physaddr;
  182. void __iomem *baseaddr;
  183. int irq;
  184. int bus_width; /* 0 := 8 bit; 1 := 16 bit */
  185. struct ace_reg_ops *reg_ops;
  186. int lock_count;
  187. /* Block device data structures */
  188. spinlock_t lock;
  189. struct device *dev;
  190. struct request_queue *queue;
  191. struct gendisk *gd;
  192. struct blk_mq_tag_set tag_set;
  193. struct list_head rq_list;
  194. /* Inserted CF card parameters */
  195. u16 cf_id[ATA_ID_WORDS];
  196. };
  197. static DEFINE_MUTEX(xsysace_mutex);
  198. static int ace_major;
  199. /* ---------------------------------------------------------------------
  200. * Low level register access
  201. */
  202. struct ace_reg_ops {
  203. u16(*in) (struct ace_device * ace, int reg);
  204. void (*out) (struct ace_device * ace, int reg, u16 val);
  205. void (*datain) (struct ace_device * ace);
  206. void (*dataout) (struct ace_device * ace);
  207. };
  208. /* 8 Bit bus width */
  209. static u16 ace_in_8(struct ace_device *ace, int reg)
  210. {
  211. void __iomem *r = ace->baseaddr + reg;
  212. return in_8(r) | (in_8(r + 1) << 8);
  213. }
  214. static void ace_out_8(struct ace_device *ace, int reg, u16 val)
  215. {
  216. void __iomem *r = ace->baseaddr + reg;
  217. out_8(r, val);
  218. out_8(r + 1, val >> 8);
  219. }
  220. static void ace_datain_8(struct ace_device *ace)
  221. {
  222. void __iomem *r = ace->baseaddr + 0x40;
  223. u8 *dst = ace->data_ptr;
  224. int i = ACE_FIFO_SIZE;
  225. while (i--)
  226. *dst++ = in_8(r++);
  227. ace->data_ptr = dst;
  228. }
  229. static void ace_dataout_8(struct ace_device *ace)
  230. {
  231. void __iomem *r = ace->baseaddr + 0x40;
  232. u8 *src = ace->data_ptr;
  233. int i = ACE_FIFO_SIZE;
  234. while (i--)
  235. out_8(r++, *src++);
  236. ace->data_ptr = src;
  237. }
  238. static struct ace_reg_ops ace_reg_8_ops = {
  239. .in = ace_in_8,
  240. .out = ace_out_8,
  241. .datain = ace_datain_8,
  242. .dataout = ace_dataout_8,
  243. };
  244. /* 16 bit big endian bus attachment */
  245. static u16 ace_in_be16(struct ace_device *ace, int reg)
  246. {
  247. return in_be16(ace->baseaddr + reg);
  248. }
  249. static void ace_out_be16(struct ace_device *ace, int reg, u16 val)
  250. {
  251. out_be16(ace->baseaddr + reg, val);
  252. }
  253. static void ace_datain_be16(struct ace_device *ace)
  254. {
  255. int i = ACE_FIFO_SIZE / 2;
  256. u16 *dst = ace->data_ptr;
  257. while (i--)
  258. *dst++ = in_le16(ace->baseaddr + 0x40);
  259. ace->data_ptr = dst;
  260. }
  261. static void ace_dataout_be16(struct ace_device *ace)
  262. {
  263. int i = ACE_FIFO_SIZE / 2;
  264. u16 *src = ace->data_ptr;
  265. while (i--)
  266. out_le16(ace->baseaddr + 0x40, *src++);
  267. ace->data_ptr = src;
  268. }
  269. /* 16 bit little endian bus attachment */
  270. static u16 ace_in_le16(struct ace_device *ace, int reg)
  271. {
  272. return in_le16(ace->baseaddr + reg);
  273. }
  274. static void ace_out_le16(struct ace_device *ace, int reg, u16 val)
  275. {
  276. out_le16(ace->baseaddr + reg, val);
  277. }
  278. static void ace_datain_le16(struct ace_device *ace)
  279. {
  280. int i = ACE_FIFO_SIZE / 2;
  281. u16 *dst = ace->data_ptr;
  282. while (i--)
  283. *dst++ = in_be16(ace->baseaddr + 0x40);
  284. ace->data_ptr = dst;
  285. }
  286. static void ace_dataout_le16(struct ace_device *ace)
  287. {
  288. int i = ACE_FIFO_SIZE / 2;
  289. u16 *src = ace->data_ptr;
  290. while (i--)
  291. out_be16(ace->baseaddr + 0x40, *src++);
  292. ace->data_ptr = src;
  293. }
  294. static struct ace_reg_ops ace_reg_be16_ops = {
  295. .in = ace_in_be16,
  296. .out = ace_out_be16,
  297. .datain = ace_datain_be16,
  298. .dataout = ace_dataout_be16,
  299. };
  300. static struct ace_reg_ops ace_reg_le16_ops = {
  301. .in = ace_in_le16,
  302. .out = ace_out_le16,
  303. .datain = ace_datain_le16,
  304. .dataout = ace_dataout_le16,
  305. };
  306. static inline u16 ace_in(struct ace_device *ace, int reg)
  307. {
  308. return ace->reg_ops->in(ace, reg);
  309. }
  310. static inline u32 ace_in32(struct ace_device *ace, int reg)
  311. {
  312. return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16);
  313. }
  314. static inline void ace_out(struct ace_device *ace, int reg, u16 val)
  315. {
  316. ace->reg_ops->out(ace, reg, val);
  317. }
  318. static inline void ace_out32(struct ace_device *ace, int reg, u32 val)
  319. {
  320. ace_out(ace, reg, val);
  321. ace_out(ace, reg + 2, val >> 16);
  322. }
  323. /* ---------------------------------------------------------------------
  324. * Debug support functions
  325. */
  326. #if defined(DEBUG)
  327. static void ace_dump_mem(void *base, int len)
  328. {
  329. const char *ptr = base;
  330. int i, j;
  331. for (i = 0; i < len; i += 16) {
  332. printk(KERN_INFO "%.8x:", i);
  333. for (j = 0; j < 16; j++) {
  334. if (!(j % 4))
  335. printk(" ");
  336. printk("%.2x", ptr[i + j]);
  337. }
  338. printk(" ");
  339. for (j = 0; j < 16; j++)
  340. printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.');
  341. printk("\n");
  342. }
  343. }
  344. #else
  345. static inline void ace_dump_mem(void *base, int len)
  346. {
  347. }
  348. #endif
  349. static void ace_dump_regs(struct ace_device *ace)
  350. {
  351. dev_info(ace->dev,
  352. " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n"
  353. " status:%.8x mpu_lba:%.8x busmode:%4x\n"
  354. " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
  355. ace_in32(ace, ACE_CTRL),
  356. ace_in(ace, ACE_SECCNTCMD),
  357. ace_in(ace, ACE_VERSION),
  358. ace_in32(ace, ACE_STATUS),
  359. ace_in32(ace, ACE_MPULBA),
  360. ace_in(ace, ACE_BUSMODE),
  361. ace_in32(ace, ACE_ERROR),
  362. ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT));
  363. }
  364. static void ace_fix_driveid(u16 *id)
  365. {
  366. #if defined(__BIG_ENDIAN)
  367. int i;
  368. /* All half words have wrong byte order; swap the bytes */
  369. for (i = 0; i < ATA_ID_WORDS; i++, id++)
  370. *id = le16_to_cpu(*id);
  371. #endif
  372. }
  373. /* ---------------------------------------------------------------------
  374. * Finite State Machine (FSM) implementation
  375. */
  376. /* FSM tasks; used to direct state transitions */
  377. #define ACE_TASK_IDLE 0
  378. #define ACE_TASK_IDENTIFY 1
  379. #define ACE_TASK_READ 2
  380. #define ACE_TASK_WRITE 3
  381. #define ACE_FSM_NUM_TASKS 4
  382. /* FSM state definitions */
  383. #define ACE_FSM_STATE_IDLE 0
  384. #define ACE_FSM_STATE_REQ_LOCK 1
  385. #define ACE_FSM_STATE_WAIT_LOCK 2
  386. #define ACE_FSM_STATE_WAIT_CFREADY 3
  387. #define ACE_FSM_STATE_IDENTIFY_PREPARE 4
  388. #define ACE_FSM_STATE_IDENTIFY_TRANSFER 5
  389. #define ACE_FSM_STATE_IDENTIFY_COMPLETE 6
  390. #define ACE_FSM_STATE_REQ_PREPARE 7
  391. #define ACE_FSM_STATE_REQ_TRANSFER 8
  392. #define ACE_FSM_STATE_REQ_COMPLETE 9
  393. #define ACE_FSM_STATE_ERROR 10
  394. #define ACE_FSM_NUM_STATES 11
  395. /* Set flag to exit FSM loop and reschedule tasklet */
  396. static inline void ace_fsm_yield(struct ace_device *ace)
  397. {
  398. dev_dbg(ace->dev, "ace_fsm_yield()\n");
  399. tasklet_schedule(&ace->fsm_tasklet);
  400. ace->fsm_continue_flag = 0;
  401. }
  402. /* Set flag to exit FSM loop and wait for IRQ to reschedule tasklet */
  403. static inline void ace_fsm_yieldirq(struct ace_device *ace)
  404. {
  405. dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
  406. if (!ace->irq)
  407. /* No IRQ assigned, so need to poll */
  408. tasklet_schedule(&ace->fsm_tasklet);
  409. ace->fsm_continue_flag = 0;
  410. }
  411. static bool ace_has_next_request(struct request_queue *q)
  412. {
  413. struct ace_device *ace = q->queuedata;
  414. return !list_empty(&ace->rq_list);
  415. }
  416. /* Get the next read/write request; ending requests that we don't handle */
  417. static struct request *ace_get_next_request(struct request_queue *q)
  418. {
  419. struct ace_device *ace = q->queuedata;
  420. struct request *rq;
  421. rq = list_first_entry_or_null(&ace->rq_list, struct request, queuelist);
  422. if (rq) {
  423. list_del_init(&rq->queuelist);
  424. blk_mq_start_request(rq);
  425. }
  426. return NULL;
  427. }
  428. static void ace_fsm_dostate(struct ace_device *ace)
  429. {
  430. struct request *req;
  431. u32 status;
  432. u16 val;
  433. int count;
  434. #if defined(DEBUG)
  435. dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n",
  436. ace->fsm_state, ace->id_req_count);
  437. #endif
  438. /* Verify that there is actually a CF in the slot. If not, then
  439. * bail out back to the idle state and wake up all the waiters */
  440. status = ace_in32(ace, ACE_STATUS);
  441. if ((status & ACE_STATUS_CFDETECT) == 0) {
  442. ace->fsm_state = ACE_FSM_STATE_IDLE;
  443. ace->media_change = 1;
  444. set_capacity(ace->gd, 0);
  445. dev_info(ace->dev, "No CF in slot\n");
  446. /* Drop all in-flight and pending requests */
  447. if (ace->req) {
  448. blk_mq_end_request(ace->req, BLK_STS_IOERR);
  449. ace->req = NULL;
  450. }
  451. while ((req = ace_get_next_request(ace->queue)) != NULL)
  452. blk_mq_end_request(req, BLK_STS_IOERR);
  453. /* Drop back to IDLE state and notify waiters */
  454. ace->fsm_state = ACE_FSM_STATE_IDLE;
  455. ace->id_result = -EIO;
  456. while (ace->id_req_count) {
  457. complete(&ace->id_completion);
  458. ace->id_req_count--;
  459. }
  460. }
  461. switch (ace->fsm_state) {
  462. case ACE_FSM_STATE_IDLE:
  463. /* See if there is anything to do */
  464. if (ace->id_req_count || ace_has_next_request(ace->queue)) {
  465. ace->fsm_iter_num++;
  466. ace->fsm_state = ACE_FSM_STATE_REQ_LOCK;
  467. mod_timer(&ace->stall_timer, jiffies + HZ);
  468. if (!timer_pending(&ace->stall_timer))
  469. add_timer(&ace->stall_timer);
  470. break;
  471. }
  472. del_timer(&ace->stall_timer);
  473. ace->fsm_continue_flag = 0;
  474. break;
  475. case ACE_FSM_STATE_REQ_LOCK:
  476. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  477. /* Already have the lock, jump to next state */
  478. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  479. break;
  480. }
  481. /* Request the lock */
  482. val = ace_in(ace, ACE_CTRL);
  483. ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ);
  484. ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK;
  485. break;
  486. case ACE_FSM_STATE_WAIT_LOCK:
  487. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  488. /* got the lock; move to next state */
  489. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  490. break;
  491. }
  492. /* wait a bit for the lock */
  493. ace_fsm_yield(ace);
  494. break;
  495. case ACE_FSM_STATE_WAIT_CFREADY:
  496. status = ace_in32(ace, ACE_STATUS);
  497. if (!(status & ACE_STATUS_RDYFORCFCMD) ||
  498. (status & ACE_STATUS_CFBSY)) {
  499. /* CF card isn't ready; it needs to be polled */
  500. ace_fsm_yield(ace);
  501. break;
  502. }
  503. /* Device is ready for command; determine what to do next */
  504. if (ace->id_req_count)
  505. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE;
  506. else
  507. ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE;
  508. break;
  509. case ACE_FSM_STATE_IDENTIFY_PREPARE:
  510. /* Send identify command */
  511. ace->fsm_task = ACE_TASK_IDENTIFY;
  512. ace->data_ptr = ace->cf_id;
  513. ace->data_count = ACE_BUF_PER_SECTOR;
  514. ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
  515. /* As per datasheet, put config controller in reset */
  516. val = ace_in(ace, ACE_CTRL);
  517. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  518. /* irq handler takes over from this point; wait for the
  519. * transfer to complete */
  520. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER;
  521. ace_fsm_yieldirq(ace);
  522. break;
  523. case ACE_FSM_STATE_IDENTIFY_TRANSFER:
  524. /* Check that the sysace is ready to receive data */
  525. status = ace_in32(ace, ACE_STATUS);
  526. if (status & ACE_STATUS_CFBSY) {
  527. dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n",
  528. ace->fsm_task, ace->fsm_iter_num,
  529. ace->data_count);
  530. ace_fsm_yield(ace);
  531. break;
  532. }
  533. if (!(status & ACE_STATUS_DATABUFRDY)) {
  534. ace_fsm_yield(ace);
  535. break;
  536. }
  537. /* Transfer the next buffer */
  538. ace->reg_ops->datain(ace);
  539. ace->data_count--;
  540. /* If there are still buffers to be transfers; jump out here */
  541. if (ace->data_count != 0) {
  542. ace_fsm_yieldirq(ace);
  543. break;
  544. }
  545. /* transfer finished; kick state machine */
  546. dev_dbg(ace->dev, "identify finished\n");
  547. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE;
  548. break;
  549. case ACE_FSM_STATE_IDENTIFY_COMPLETE:
  550. ace_fix_driveid(ace->cf_id);
  551. ace_dump_mem(ace->cf_id, 512); /* Debug: Dump out disk ID */
  552. if (ace->data_result) {
  553. /* Error occurred, disable the disk */
  554. ace->media_change = 1;
  555. set_capacity(ace->gd, 0);
  556. dev_err(ace->dev, "error fetching CF id (%i)\n",
  557. ace->data_result);
  558. } else {
  559. ace->media_change = 0;
  560. /* Record disk parameters */
  561. set_capacity(ace->gd,
  562. ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
  563. dev_info(ace->dev, "capacity: %i sectors\n",
  564. ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
  565. }
  566. /* We're done, drop to IDLE state and notify waiters */
  567. ace->fsm_state = ACE_FSM_STATE_IDLE;
  568. ace->id_result = ace->data_result;
  569. while (ace->id_req_count) {
  570. complete(&ace->id_completion);
  571. ace->id_req_count--;
  572. }
  573. break;
  574. case ACE_FSM_STATE_REQ_PREPARE:
  575. req = ace_get_next_request(ace->queue);
  576. if (!req) {
  577. ace->fsm_state = ACE_FSM_STATE_IDLE;
  578. break;
  579. }
  580. /* Okay, it's a data request, set it up for transfer */
  581. dev_dbg(ace->dev,
  582. "request: sec=%llx hcnt=%x, ccnt=%x, dir=%i\n",
  583. (unsigned long long)blk_rq_pos(req),
  584. blk_rq_sectors(req), blk_rq_cur_sectors(req),
  585. rq_data_dir(req));
  586. ace->req = req;
  587. ace->data_ptr = bio_data(req->bio);
  588. ace->data_count = blk_rq_cur_sectors(req) * ACE_BUF_PER_SECTOR;
  589. ace_out32(ace, ACE_MPULBA, blk_rq_pos(req) & 0x0FFFFFFF);
  590. count = blk_rq_sectors(req);
  591. if (rq_data_dir(req)) {
  592. /* Kick off write request */
  593. dev_dbg(ace->dev, "write data\n");
  594. ace->fsm_task = ACE_TASK_WRITE;
  595. ace_out(ace, ACE_SECCNTCMD,
  596. count | ACE_SECCNTCMD_WRITE_DATA);
  597. } else {
  598. /* Kick off read request */
  599. dev_dbg(ace->dev, "read data\n");
  600. ace->fsm_task = ACE_TASK_READ;
  601. ace_out(ace, ACE_SECCNTCMD,
  602. count | ACE_SECCNTCMD_READ_DATA);
  603. }
  604. /* As per datasheet, put config controller in reset */
  605. val = ace_in(ace, ACE_CTRL);
  606. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  607. /* Move to the transfer state. The systemace will raise
  608. * an interrupt once there is something to do
  609. */
  610. ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
  611. if (ace->fsm_task == ACE_TASK_READ)
  612. ace_fsm_yieldirq(ace); /* wait for data ready */
  613. break;
  614. case ACE_FSM_STATE_REQ_TRANSFER:
  615. /* Check that the sysace is ready to receive data */
  616. status = ace_in32(ace, ACE_STATUS);
  617. if (status & ACE_STATUS_CFBSY) {
  618. dev_dbg(ace->dev,
  619. "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  620. ace->fsm_task, ace->fsm_iter_num,
  621. blk_rq_cur_sectors(ace->req) * 16,
  622. ace->data_count, ace->in_irq);
  623. ace_fsm_yield(ace); /* need to poll CFBSY bit */
  624. break;
  625. }
  626. if (!(status & ACE_STATUS_DATABUFRDY)) {
  627. dev_dbg(ace->dev,
  628. "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  629. ace->fsm_task, ace->fsm_iter_num,
  630. blk_rq_cur_sectors(ace->req) * 16,
  631. ace->data_count, ace->in_irq);
  632. ace_fsm_yieldirq(ace);
  633. break;
  634. }
  635. /* Transfer the next buffer */
  636. if (ace->fsm_task == ACE_TASK_WRITE)
  637. ace->reg_ops->dataout(ace);
  638. else
  639. ace->reg_ops->datain(ace);
  640. ace->data_count--;
  641. /* If there are still buffers to be transfers; jump out here */
  642. if (ace->data_count != 0) {
  643. ace_fsm_yieldirq(ace);
  644. break;
  645. }
  646. /* bio finished; is there another one? */
  647. if (blk_update_request(ace->req, BLK_STS_OK,
  648. blk_rq_cur_bytes(ace->req))) {
  649. /* dev_dbg(ace->dev, "next block; h=%u c=%u\n",
  650. * blk_rq_sectors(ace->req),
  651. * blk_rq_cur_sectors(ace->req));
  652. */
  653. ace->data_ptr = bio_data(ace->req->bio);
  654. ace->data_count = blk_rq_cur_sectors(ace->req) * 16;
  655. ace_fsm_yieldirq(ace);
  656. break;
  657. }
  658. ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
  659. break;
  660. case ACE_FSM_STATE_REQ_COMPLETE:
  661. ace->req = NULL;
  662. /* Finished request; go to idle state */
  663. ace->fsm_state = ACE_FSM_STATE_IDLE;
  664. break;
  665. default:
  666. ace->fsm_state = ACE_FSM_STATE_IDLE;
  667. break;
  668. }
  669. }
  670. static void ace_fsm_tasklet(unsigned long data)
  671. {
  672. struct ace_device *ace = (void *)data;
  673. unsigned long flags;
  674. spin_lock_irqsave(&ace->lock, flags);
  675. /* Loop over state machine until told to stop */
  676. ace->fsm_continue_flag = 1;
  677. while (ace->fsm_continue_flag)
  678. ace_fsm_dostate(ace);
  679. spin_unlock_irqrestore(&ace->lock, flags);
  680. }
  681. static void ace_stall_timer(struct timer_list *t)
  682. {
  683. struct ace_device *ace = from_timer(ace, t, stall_timer);
  684. unsigned long flags;
  685. dev_warn(ace->dev,
  686. "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
  687. ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
  688. ace->data_count);
  689. spin_lock_irqsave(&ace->lock, flags);
  690. /* Rearm the stall timer *before* entering FSM (which may then
  691. * delete the timer) */
  692. mod_timer(&ace->stall_timer, jiffies + HZ);
  693. /* Loop over state machine until told to stop */
  694. ace->fsm_continue_flag = 1;
  695. while (ace->fsm_continue_flag)
  696. ace_fsm_dostate(ace);
  697. spin_unlock_irqrestore(&ace->lock, flags);
  698. }
  699. /* ---------------------------------------------------------------------
  700. * Interrupt handling routines
  701. */
  702. static int ace_interrupt_checkstate(struct ace_device *ace)
  703. {
  704. u32 sreg = ace_in32(ace, ACE_STATUS);
  705. u16 creg = ace_in(ace, ACE_CTRL);
  706. /* Check for error occurrence */
  707. if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
  708. (creg & ACE_CTRL_ERRORIRQ)) {
  709. dev_err(ace->dev, "transfer failure\n");
  710. ace_dump_regs(ace);
  711. return -EIO;
  712. }
  713. return 0;
  714. }
  715. static irqreturn_t ace_interrupt(int irq, void *dev_id)
  716. {
  717. u16 creg;
  718. struct ace_device *ace = dev_id;
  719. /* be safe and get the lock */
  720. spin_lock(&ace->lock);
  721. ace->in_irq = 1;
  722. /* clear the interrupt */
  723. creg = ace_in(ace, ACE_CTRL);
  724. ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
  725. ace_out(ace, ACE_CTRL, creg);
  726. /* check for IO failures */
  727. if (ace_interrupt_checkstate(ace))
  728. ace->data_result = -EIO;
  729. if (ace->fsm_task == 0) {
  730. dev_err(ace->dev,
  731. "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
  732. ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
  733. ace_in(ace, ACE_SECCNTCMD));
  734. dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
  735. ace->fsm_task, ace->fsm_state, ace->data_count);
  736. }
  737. /* Loop over state machine until told to stop */
  738. ace->fsm_continue_flag = 1;
  739. while (ace->fsm_continue_flag)
  740. ace_fsm_dostate(ace);
  741. /* done with interrupt; drop the lock */
  742. ace->in_irq = 0;
  743. spin_unlock(&ace->lock);
  744. return IRQ_HANDLED;
  745. }
  746. /* ---------------------------------------------------------------------
  747. * Block ops
  748. */
  749. static blk_status_t ace_queue_rq(struct blk_mq_hw_ctx *hctx,
  750. const struct blk_mq_queue_data *bd)
  751. {
  752. struct ace_device *ace = hctx->queue->queuedata;
  753. struct request *req = bd->rq;
  754. if (blk_rq_is_passthrough(req)) {
  755. blk_mq_start_request(req);
  756. return BLK_STS_IOERR;
  757. }
  758. spin_lock_irq(&ace->lock);
  759. list_add_tail(&req->queuelist, &ace->rq_list);
  760. spin_unlock_irq(&ace->lock);
  761. tasklet_schedule(&ace->fsm_tasklet);
  762. return BLK_STS_OK;
  763. }
  764. static unsigned int ace_check_events(struct gendisk *gd, unsigned int clearing)
  765. {
  766. struct ace_device *ace = gd->private_data;
  767. dev_dbg(ace->dev, "ace_check_events(): %i\n", ace->media_change);
  768. return ace->media_change ? DISK_EVENT_MEDIA_CHANGE : 0;
  769. }
  770. static int ace_revalidate_disk(struct gendisk *gd)
  771. {
  772. struct ace_device *ace = gd->private_data;
  773. unsigned long flags;
  774. dev_dbg(ace->dev, "ace_revalidate_disk()\n");
  775. if (ace->media_change) {
  776. dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
  777. spin_lock_irqsave(&ace->lock, flags);
  778. ace->id_req_count++;
  779. spin_unlock_irqrestore(&ace->lock, flags);
  780. tasklet_schedule(&ace->fsm_tasklet);
  781. wait_for_completion(&ace->id_completion);
  782. }
  783. dev_dbg(ace->dev, "revalidate complete\n");
  784. return ace->id_result;
  785. }
  786. static int ace_open(struct block_device *bdev, fmode_t mode)
  787. {
  788. struct ace_device *ace = bdev->bd_disk->private_data;
  789. unsigned long flags;
  790. dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
  791. mutex_lock(&xsysace_mutex);
  792. spin_lock_irqsave(&ace->lock, flags);
  793. ace->users++;
  794. spin_unlock_irqrestore(&ace->lock, flags);
  795. check_disk_change(bdev);
  796. mutex_unlock(&xsysace_mutex);
  797. return 0;
  798. }
  799. static void ace_release(struct gendisk *disk, fmode_t mode)
  800. {
  801. struct ace_device *ace = disk->private_data;
  802. unsigned long flags;
  803. u16 val;
  804. dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
  805. mutex_lock(&xsysace_mutex);
  806. spin_lock_irqsave(&ace->lock, flags);
  807. ace->users--;
  808. if (ace->users == 0) {
  809. val = ace_in(ace, ACE_CTRL);
  810. ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
  811. }
  812. spin_unlock_irqrestore(&ace->lock, flags);
  813. mutex_unlock(&xsysace_mutex);
  814. }
  815. static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  816. {
  817. struct ace_device *ace = bdev->bd_disk->private_data;
  818. u16 *cf_id = ace->cf_id;
  819. dev_dbg(ace->dev, "ace_getgeo()\n");
  820. geo->heads = cf_id[ATA_ID_HEADS];
  821. geo->sectors = cf_id[ATA_ID_SECTORS];
  822. geo->cylinders = cf_id[ATA_ID_CYLS];
  823. return 0;
  824. }
  825. static const struct block_device_operations ace_fops = {
  826. .owner = THIS_MODULE,
  827. .open = ace_open,
  828. .release = ace_release,
  829. .check_events = ace_check_events,
  830. .revalidate_disk = ace_revalidate_disk,
  831. .getgeo = ace_getgeo,
  832. };
  833. static const struct blk_mq_ops ace_mq_ops = {
  834. .queue_rq = ace_queue_rq,
  835. };
  836. /* --------------------------------------------------------------------
  837. * SystemACE device setup/teardown code
  838. */
  839. static int ace_setup(struct ace_device *ace)
  840. {
  841. u16 version;
  842. u16 val;
  843. int rc;
  844. dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace);
  845. dev_dbg(ace->dev, "physaddr=0x%llx irq=%i\n",
  846. (unsigned long long)ace->physaddr, ace->irq);
  847. spin_lock_init(&ace->lock);
  848. init_completion(&ace->id_completion);
  849. INIT_LIST_HEAD(&ace->rq_list);
  850. /*
  851. * Map the device
  852. */
  853. ace->baseaddr = ioremap(ace->physaddr, 0x80);
  854. if (!ace->baseaddr)
  855. goto err_ioremap;
  856. /*
  857. * Initialize the state machine tasklet and stall timer
  858. */
  859. tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
  860. timer_setup(&ace->stall_timer, ace_stall_timer, 0);
  861. /*
  862. * Initialize the request queue
  863. */
  864. ace->queue = blk_mq_init_sq_queue(&ace->tag_set, &ace_mq_ops, 2,
  865. BLK_MQ_F_SHOULD_MERGE);
  866. if (IS_ERR(ace->queue)) {
  867. rc = PTR_ERR(ace->queue);
  868. ace->queue = NULL;
  869. goto err_blk_initq;
  870. }
  871. ace->queue->queuedata = ace;
  872. blk_queue_logical_block_size(ace->queue, 512);
  873. blk_queue_bounce_limit(ace->queue, BLK_BOUNCE_HIGH);
  874. /*
  875. * Allocate and initialize GD structure
  876. */
  877. ace->gd = alloc_disk(ACE_NUM_MINORS);
  878. if (!ace->gd)
  879. goto err_alloc_disk;
  880. ace->gd->major = ace_major;
  881. ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
  882. ace->gd->fops = &ace_fops;
  883. ace->gd->queue = ace->queue;
  884. ace->gd->private_data = ace;
  885. snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
  886. /* set bus width */
  887. if (ace->bus_width == ACE_BUS_WIDTH_16) {
  888. /* 0x0101 should work regardless of endianess */
  889. ace_out_le16(ace, ACE_BUSMODE, 0x0101);
  890. /* read it back to determine endianess */
  891. if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
  892. ace->reg_ops = &ace_reg_le16_ops;
  893. else
  894. ace->reg_ops = &ace_reg_be16_ops;
  895. } else {
  896. ace_out_8(ace, ACE_BUSMODE, 0x00);
  897. ace->reg_ops = &ace_reg_8_ops;
  898. }
  899. /* Make sure version register is sane */
  900. version = ace_in(ace, ACE_VERSION);
  901. if ((version == 0) || (version == 0xFFFF))
  902. goto err_read;
  903. /* Put sysace in a sane state by clearing most control reg bits */
  904. ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
  905. ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
  906. /* Now we can hook up the irq handler */
  907. if (ace->irq) {
  908. rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
  909. if (rc) {
  910. /* Failure - fall back to polled mode */
  911. dev_err(ace->dev, "request_irq failed\n");
  912. ace->irq = 0;
  913. }
  914. }
  915. /* Enable interrupts */
  916. val = ace_in(ace, ACE_CTRL);
  917. val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
  918. ace_out(ace, ACE_CTRL, val);
  919. /* Print the identification */
  920. dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
  921. (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
  922. dev_dbg(ace->dev, "physaddr 0x%llx, mapped to 0x%p, irq=%i\n",
  923. (unsigned long long) ace->physaddr, ace->baseaddr, ace->irq);
  924. ace->media_change = 1;
  925. ace_revalidate_disk(ace->gd);
  926. /* Make the sysace device 'live' */
  927. add_disk(ace->gd);
  928. return 0;
  929. err_read:
  930. put_disk(ace->gd);
  931. err_alloc_disk:
  932. blk_cleanup_queue(ace->queue);
  933. blk_mq_free_tag_set(&ace->tag_set);
  934. err_blk_initq:
  935. iounmap(ace->baseaddr);
  936. err_ioremap:
  937. dev_info(ace->dev, "xsysace: error initializing device at 0x%llx\n",
  938. (unsigned long long) ace->physaddr);
  939. return -ENOMEM;
  940. }
  941. static void ace_teardown(struct ace_device *ace)
  942. {
  943. if (ace->gd) {
  944. del_gendisk(ace->gd);
  945. put_disk(ace->gd);
  946. }
  947. if (ace->queue) {
  948. blk_cleanup_queue(ace->queue);
  949. blk_mq_free_tag_set(&ace->tag_set);
  950. }
  951. tasklet_kill(&ace->fsm_tasklet);
  952. if (ace->irq)
  953. free_irq(ace->irq, ace);
  954. iounmap(ace->baseaddr);
  955. }
  956. static int ace_alloc(struct device *dev, int id, resource_size_t physaddr,
  957. int irq, int bus_width)
  958. {
  959. struct ace_device *ace;
  960. int rc;
  961. dev_dbg(dev, "ace_alloc(%p)\n", dev);
  962. if (!physaddr) {
  963. rc = -ENODEV;
  964. goto err_noreg;
  965. }
  966. /* Allocate and initialize the ace device structure */
  967. ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
  968. if (!ace) {
  969. rc = -ENOMEM;
  970. goto err_alloc;
  971. }
  972. ace->dev = dev;
  973. ace->id = id;
  974. ace->physaddr = physaddr;
  975. ace->irq = irq;
  976. ace->bus_width = bus_width;
  977. /* Call the setup code */
  978. rc = ace_setup(ace);
  979. if (rc)
  980. goto err_setup;
  981. dev_set_drvdata(dev, ace);
  982. return 0;
  983. err_setup:
  984. dev_set_drvdata(dev, NULL);
  985. kfree(ace);
  986. err_alloc:
  987. err_noreg:
  988. dev_err(dev, "could not initialize device, err=%i\n", rc);
  989. return rc;
  990. }
  991. static void ace_free(struct device *dev)
  992. {
  993. struct ace_device *ace = dev_get_drvdata(dev);
  994. dev_dbg(dev, "ace_free(%p)\n", dev);
  995. if (ace) {
  996. ace_teardown(ace);
  997. dev_set_drvdata(dev, NULL);
  998. kfree(ace);
  999. }
  1000. }
  1001. /* ---------------------------------------------------------------------
  1002. * Platform Bus Support
  1003. */
  1004. static int ace_probe(struct platform_device *dev)
  1005. {
  1006. resource_size_t physaddr = 0;
  1007. int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */
  1008. u32 id = dev->id;
  1009. int irq = 0;
  1010. int i;
  1011. dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
  1012. /* device id and bus width */
  1013. if (of_property_read_u32(dev->dev.of_node, "port-number", &id))
  1014. id = 0;
  1015. if (of_find_property(dev->dev.of_node, "8-bit", NULL))
  1016. bus_width = ACE_BUS_WIDTH_8;
  1017. for (i = 0; i < dev->num_resources; i++) {
  1018. if (dev->resource[i].flags & IORESOURCE_MEM)
  1019. physaddr = dev->resource[i].start;
  1020. if (dev->resource[i].flags & IORESOURCE_IRQ)
  1021. irq = dev->resource[i].start;
  1022. }
  1023. /* Call the bus-independent setup code */
  1024. return ace_alloc(&dev->dev, id, physaddr, irq, bus_width);
  1025. }
  1026. /*
  1027. * Platform bus remove() method
  1028. */
  1029. static int ace_remove(struct platform_device *dev)
  1030. {
  1031. ace_free(&dev->dev);
  1032. return 0;
  1033. }
  1034. #if defined(CONFIG_OF)
  1035. /* Match table for of_platform binding */
  1036. static const struct of_device_id ace_of_match[] = {
  1037. { .compatible = "xlnx,opb-sysace-1.00.b", },
  1038. { .compatible = "xlnx,opb-sysace-1.00.c", },
  1039. { .compatible = "xlnx,xps-sysace-1.00.a", },
  1040. { .compatible = "xlnx,sysace", },
  1041. {},
  1042. };
  1043. MODULE_DEVICE_TABLE(of, ace_of_match);
  1044. #else /* CONFIG_OF */
  1045. #define ace_of_match NULL
  1046. #endif /* CONFIG_OF */
  1047. static struct platform_driver ace_platform_driver = {
  1048. .probe = ace_probe,
  1049. .remove = ace_remove,
  1050. .driver = {
  1051. .name = "xsysace",
  1052. .of_match_table = ace_of_match,
  1053. },
  1054. };
  1055. /* ---------------------------------------------------------------------
  1056. * Module init/exit routines
  1057. */
  1058. static int __init ace_init(void)
  1059. {
  1060. int rc;
  1061. ace_major = register_blkdev(ace_major, "xsysace");
  1062. if (ace_major <= 0) {
  1063. rc = -ENOMEM;
  1064. goto err_blk;
  1065. }
  1066. rc = platform_driver_register(&ace_platform_driver);
  1067. if (rc)
  1068. goto err_plat;
  1069. pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
  1070. return 0;
  1071. err_plat:
  1072. unregister_blkdev(ace_major, "xsysace");
  1073. err_blk:
  1074. printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
  1075. return rc;
  1076. }
  1077. module_init(ace_init);
  1078. static void __exit ace_exit(void)
  1079. {
  1080. pr_debug("Unregistering Xilinx SystemACE driver\n");
  1081. platform_driver_unregister(&ace_platform_driver);
  1082. unregister_blkdev(ace_major, "xsysace");
  1083. }
  1084. module_exit(ace_exit);