xsysace.c 34 KB

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