xsysace.c 34 KB

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