ep93xx_spi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /*
  2. * Driver for Cirrus Logic EP93xx SPI controller.
  3. *
  4. * Copyright (c) 2010 Mika Westerberg
  5. *
  6. * Explicit FIFO handling code was inspired by amba-pl022 driver.
  7. *
  8. * Chip select support using other than built-in GPIOs by H. Hartley Sweeten.
  9. *
  10. * For more information about the SPI controller see documentation on Cirrus
  11. * Logic web site:
  12. * http://www.cirrus.com/en/pubs/manual/EP93xx_Users_Guide_UM1.pdf
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/bitops.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/sched.h>
  28. #include <linux/spi/spi.h>
  29. #include <mach/ep93xx_spi.h>
  30. #define SSPCR0 0x0000
  31. #define SSPCR0_MODE_SHIFT 6
  32. #define SSPCR0_SCR_SHIFT 8
  33. #define SSPCR1 0x0004
  34. #define SSPCR1_RIE BIT(0)
  35. #define SSPCR1_TIE BIT(1)
  36. #define SSPCR1_RORIE BIT(2)
  37. #define SSPCR1_LBM BIT(3)
  38. #define SSPCR1_SSE BIT(4)
  39. #define SSPCR1_MS BIT(5)
  40. #define SSPCR1_SOD BIT(6)
  41. #define SSPDR 0x0008
  42. #define SSPSR 0x000c
  43. #define SSPSR_TFE BIT(0)
  44. #define SSPSR_TNF BIT(1)
  45. #define SSPSR_RNE BIT(2)
  46. #define SSPSR_RFF BIT(3)
  47. #define SSPSR_BSY BIT(4)
  48. #define SSPCPSR 0x0010
  49. #define SSPIIR 0x0014
  50. #define SSPIIR_RIS BIT(0)
  51. #define SSPIIR_TIS BIT(1)
  52. #define SSPIIR_RORIS BIT(2)
  53. #define SSPICR SSPIIR
  54. /* timeout in milliseconds */
  55. #define SPI_TIMEOUT 5
  56. /* maximum depth of RX/TX FIFO */
  57. #define SPI_FIFO_SIZE 8
  58. /**
  59. * struct ep93xx_spi - EP93xx SPI controller structure
  60. * @lock: spinlock that protects concurrent accesses to fields @running,
  61. * @current_msg and @msg_queue
  62. * @pdev: pointer to platform device
  63. * @clk: clock for the controller
  64. * @regs_base: pointer to ioremap()'d registers
  65. * @irq: IRQ number used by the driver
  66. * @min_rate: minimum clock rate (in Hz) supported by the controller
  67. * @max_rate: maximum clock rate (in Hz) supported by the controller
  68. * @running: is the queue running
  69. * @wq: workqueue used by the driver
  70. * @msg_work: work that is queued for the driver
  71. * @wait: wait here until given transfer is completed
  72. * @msg_queue: queue for the messages
  73. * @current_msg: message that is currently processed (or %NULL if none)
  74. * @tx: current byte in transfer to transmit
  75. * @rx: current byte in transfer to receive
  76. * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one
  77. * frame decreases this level and sending one frame increases it.
  78. *
  79. * This structure holds EP93xx SPI controller specific information. When
  80. * @running is %true, driver accepts transfer requests from protocol drivers.
  81. * @current_msg is used to hold pointer to the message that is currently
  82. * processed. If @current_msg is %NULL, it means that no processing is going
  83. * on.
  84. *
  85. * Most of the fields are only written once and they can be accessed without
  86. * taking the @lock. Fields that are accessed concurrently are: @current_msg,
  87. * @running, and @msg_queue.
  88. */
  89. struct ep93xx_spi {
  90. spinlock_t lock;
  91. const struct platform_device *pdev;
  92. struct clk *clk;
  93. void __iomem *regs_base;
  94. int irq;
  95. unsigned long min_rate;
  96. unsigned long max_rate;
  97. bool running;
  98. struct workqueue_struct *wq;
  99. struct work_struct msg_work;
  100. struct completion wait;
  101. struct list_head msg_queue;
  102. struct spi_message *current_msg;
  103. size_t tx;
  104. size_t rx;
  105. size_t fifo_level;
  106. };
  107. /**
  108. * struct ep93xx_spi_chip - SPI device hardware settings
  109. * @spi: back pointer to the SPI device
  110. * @rate: max rate in hz this chip supports
  111. * @div_cpsr: cpsr (pre-scaler) divider
  112. * @div_scr: scr divider
  113. * @dss: bits per word (4 - 16 bits)
  114. * @ops: private chip operations
  115. *
  116. * This structure is used to store hardware register specific settings for each
  117. * SPI device. Settings are written to hardware by function
  118. * ep93xx_spi_chip_setup().
  119. */
  120. struct ep93xx_spi_chip {
  121. const struct spi_device *spi;
  122. unsigned long rate;
  123. u8 div_cpsr;
  124. u8 div_scr;
  125. u8 dss;
  126. struct ep93xx_spi_chip_ops *ops;
  127. };
  128. /* converts bits per word to CR0.DSS value */
  129. #define bits_per_word_to_dss(bpw) ((bpw) - 1)
  130. static inline void
  131. ep93xx_spi_write_u8(const struct ep93xx_spi *espi, u16 reg, u8 value)
  132. {
  133. __raw_writeb(value, espi->regs_base + reg);
  134. }
  135. static inline u8
  136. ep93xx_spi_read_u8(const struct ep93xx_spi *spi, u16 reg)
  137. {
  138. return __raw_readb(spi->regs_base + reg);
  139. }
  140. static inline void
  141. ep93xx_spi_write_u16(const struct ep93xx_spi *espi, u16 reg, u16 value)
  142. {
  143. __raw_writew(value, espi->regs_base + reg);
  144. }
  145. static inline u16
  146. ep93xx_spi_read_u16(const struct ep93xx_spi *spi, u16 reg)
  147. {
  148. return __raw_readw(spi->regs_base + reg);
  149. }
  150. static int ep93xx_spi_enable(const struct ep93xx_spi *espi)
  151. {
  152. u8 regval;
  153. int err;
  154. err = clk_enable(espi->clk);
  155. if (err)
  156. return err;
  157. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  158. regval |= SSPCR1_SSE;
  159. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  160. return 0;
  161. }
  162. static void ep93xx_spi_disable(const struct ep93xx_spi *espi)
  163. {
  164. u8 regval;
  165. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  166. regval &= ~SSPCR1_SSE;
  167. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  168. clk_disable(espi->clk);
  169. }
  170. static void ep93xx_spi_enable_interrupts(const struct ep93xx_spi *espi)
  171. {
  172. u8 regval;
  173. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  174. regval |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  175. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  176. }
  177. static void ep93xx_spi_disable_interrupts(const struct ep93xx_spi *espi)
  178. {
  179. u8 regval;
  180. regval = ep93xx_spi_read_u8(espi, SSPCR1);
  181. regval &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
  182. ep93xx_spi_write_u8(espi, SSPCR1, regval);
  183. }
  184. /**
  185. * ep93xx_spi_calc_divisors() - calculates SPI clock divisors
  186. * @espi: ep93xx SPI controller struct
  187. * @chip: divisors are calculated for this chip
  188. * @rate: desired SPI output clock rate
  189. *
  190. * Function calculates cpsr (clock pre-scaler) and scr divisors based on
  191. * given @rate and places them to @chip->div_cpsr and @chip->div_scr. If,
  192. * for some reason, divisors cannot be calculated nothing is stored and
  193. * %-EINVAL is returned.
  194. */
  195. static int ep93xx_spi_calc_divisors(const struct ep93xx_spi *espi,
  196. struct ep93xx_spi_chip *chip,
  197. unsigned long rate)
  198. {
  199. unsigned long spi_clk_rate = clk_get_rate(espi->clk);
  200. int cpsr, scr;
  201. /*
  202. * Make sure that max value is between values supported by the
  203. * controller. Note that minimum value is already checked in
  204. * ep93xx_spi_transfer().
  205. */
  206. rate = clamp(rate, espi->min_rate, espi->max_rate);
  207. /*
  208. * Calculate divisors so that we can get speed according the
  209. * following formula:
  210. * rate = spi_clock_rate / (cpsr * (1 + scr))
  211. *
  212. * cpsr must be even number and starts from 2, scr can be any number
  213. * between 0 and 255.
  214. */
  215. for (cpsr = 2; cpsr <= 254; cpsr += 2) {
  216. for (scr = 0; scr <= 255; scr++) {
  217. if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) {
  218. chip->div_scr = (u8)scr;
  219. chip->div_cpsr = (u8)cpsr;
  220. return 0;
  221. }
  222. }
  223. }
  224. return -EINVAL;
  225. }
  226. static void ep93xx_spi_cs_control(struct spi_device *spi, bool control)
  227. {
  228. struct ep93xx_spi_chip *chip = spi_get_ctldata(spi);
  229. int value = (spi->mode & SPI_CS_HIGH) ? control : !control;
  230. if (chip->ops && chip->ops->cs_control)
  231. chip->ops->cs_control(spi, value);
  232. }
  233. /**
  234. * ep93xx_spi_setup() - setup an SPI device
  235. * @spi: SPI device to setup
  236. *
  237. * This function sets up SPI device mode, speed etc. Can be called multiple
  238. * times for a single device. Returns %0 in case of success, negative error in
  239. * case of failure. When this function returns success, the device is
  240. * deselected.
  241. */
  242. static int ep93xx_spi_setup(struct spi_device *spi)
  243. {
  244. struct ep93xx_spi *espi = spi_master_get_devdata(spi->master);
  245. struct ep93xx_spi_chip *chip;
  246. if (spi->bits_per_word < 4 || spi->bits_per_word > 16) {
  247. dev_err(&espi->pdev->dev, "invalid bits per word %d\n",
  248. spi->bits_per_word);
  249. return -EINVAL;
  250. }
  251. chip = spi_get_ctldata(spi);
  252. if (!chip) {
  253. dev_dbg(&espi->pdev->dev, "initial setup for %s\n",
  254. spi->modalias);
  255. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  256. if (!chip)
  257. return -ENOMEM;
  258. chip->spi = spi;
  259. chip->ops = spi->controller_data;
  260. if (chip->ops && chip->ops->setup) {
  261. int ret = chip->ops->setup(spi);
  262. if (ret) {
  263. kfree(chip);
  264. return ret;
  265. }
  266. }
  267. spi_set_ctldata(spi, chip);
  268. }
  269. if (spi->max_speed_hz != chip->rate) {
  270. int err;
  271. err = ep93xx_spi_calc_divisors(espi, chip, spi->max_speed_hz);
  272. if (err != 0) {
  273. spi_set_ctldata(spi, NULL);
  274. kfree(chip);
  275. return err;
  276. }
  277. chip->rate = spi->max_speed_hz;
  278. }
  279. chip->dss = bits_per_word_to_dss(spi->bits_per_word);
  280. ep93xx_spi_cs_control(spi, false);
  281. return 0;
  282. }
  283. /**
  284. * ep93xx_spi_transfer() - queue message to be transferred
  285. * @spi: target SPI device
  286. * @msg: message to be transferred
  287. *
  288. * This function is called by SPI device drivers when they are going to transfer
  289. * a new message. It simply puts the message in the queue and schedules
  290. * workqueue to perform the actual transfer later on.
  291. *
  292. * Returns %0 on success and negative error in case of failure.
  293. */
  294. static int ep93xx_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  295. {
  296. struct ep93xx_spi *espi = spi_master_get_devdata(spi->master);
  297. struct spi_transfer *t;
  298. unsigned long flags;
  299. if (!msg || !msg->complete)
  300. return -EINVAL;
  301. /* first validate each transfer */
  302. list_for_each_entry(t, &msg->transfers, transfer_list) {
  303. if (t->bits_per_word) {
  304. if (t->bits_per_word < 4 || t->bits_per_word > 16)
  305. return -EINVAL;
  306. }
  307. if (t->speed_hz && t->speed_hz < espi->min_rate)
  308. return -EINVAL;
  309. }
  310. /*
  311. * Now that we own the message, let's initialize it so that it is
  312. * suitable for us. We use @msg->status to signal whether there was
  313. * error in transfer and @msg->state is used to hold pointer to the
  314. * current transfer (or %NULL if no active current transfer).
  315. */
  316. msg->state = NULL;
  317. msg->status = 0;
  318. msg->actual_length = 0;
  319. spin_lock_irqsave(&espi->lock, flags);
  320. if (!espi->running) {
  321. spin_unlock_irqrestore(&espi->lock, flags);
  322. return -ESHUTDOWN;
  323. }
  324. list_add_tail(&msg->queue, &espi->msg_queue);
  325. queue_work(espi->wq, &espi->msg_work);
  326. spin_unlock_irqrestore(&espi->lock, flags);
  327. return 0;
  328. }
  329. /**
  330. * ep93xx_spi_cleanup() - cleans up master controller specific state
  331. * @spi: SPI device to cleanup
  332. *
  333. * This function releases master controller specific state for given @spi
  334. * device.
  335. */
  336. static void ep93xx_spi_cleanup(struct spi_device *spi)
  337. {
  338. struct ep93xx_spi_chip *chip;
  339. chip = spi_get_ctldata(spi);
  340. if (chip) {
  341. if (chip->ops && chip->ops->cleanup)
  342. chip->ops->cleanup(spi);
  343. spi_set_ctldata(spi, NULL);
  344. kfree(chip);
  345. }
  346. }
  347. /**
  348. * ep93xx_spi_chip_setup() - configures hardware according to given @chip
  349. * @espi: ep93xx SPI controller struct
  350. * @chip: chip specific settings
  351. *
  352. * This function sets up the actual hardware registers with settings given in
  353. * @chip. Note that no validation is done so make sure that callers validate
  354. * settings before calling this.
  355. */
  356. static void ep93xx_spi_chip_setup(const struct ep93xx_spi *espi,
  357. const struct ep93xx_spi_chip *chip)
  358. {
  359. u16 cr0;
  360. cr0 = chip->div_scr << SSPCR0_SCR_SHIFT;
  361. cr0 |= (chip->spi->mode & (SPI_CPHA|SPI_CPOL)) << SSPCR0_MODE_SHIFT;
  362. cr0 |= chip->dss;
  363. dev_dbg(&espi->pdev->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
  364. chip->spi->mode, chip->div_cpsr, chip->div_scr, chip->dss);
  365. dev_dbg(&espi->pdev->dev, "setup: cr0 %#x", cr0);
  366. ep93xx_spi_write_u8(espi, SSPCPSR, chip->div_cpsr);
  367. ep93xx_spi_write_u16(espi, SSPCR0, cr0);
  368. }
  369. static inline int bits_per_word(const struct ep93xx_spi *espi)
  370. {
  371. struct spi_message *msg = espi->current_msg;
  372. struct spi_transfer *t = msg->state;
  373. return t->bits_per_word ? t->bits_per_word : msg->spi->bits_per_word;
  374. }
  375. static void ep93xx_do_write(struct ep93xx_spi *espi, struct spi_transfer *t)
  376. {
  377. if (bits_per_word(espi) > 8) {
  378. u16 tx_val = 0;
  379. if (t->tx_buf)
  380. tx_val = ((u16 *)t->tx_buf)[espi->tx];
  381. ep93xx_spi_write_u16(espi, SSPDR, tx_val);
  382. espi->tx += sizeof(tx_val);
  383. } else {
  384. u8 tx_val = 0;
  385. if (t->tx_buf)
  386. tx_val = ((u8 *)t->tx_buf)[espi->tx];
  387. ep93xx_spi_write_u8(espi, SSPDR, tx_val);
  388. espi->tx += sizeof(tx_val);
  389. }
  390. }
  391. static void ep93xx_do_read(struct ep93xx_spi *espi, struct spi_transfer *t)
  392. {
  393. if (bits_per_word(espi) > 8) {
  394. u16 rx_val;
  395. rx_val = ep93xx_spi_read_u16(espi, SSPDR);
  396. if (t->rx_buf)
  397. ((u16 *)t->rx_buf)[espi->rx] = rx_val;
  398. espi->rx += sizeof(rx_val);
  399. } else {
  400. u8 rx_val;
  401. rx_val = ep93xx_spi_read_u8(espi, SSPDR);
  402. if (t->rx_buf)
  403. ((u8 *)t->rx_buf)[espi->rx] = rx_val;
  404. espi->rx += sizeof(rx_val);
  405. }
  406. }
  407. /**
  408. * ep93xx_spi_read_write() - perform next RX/TX transfer
  409. * @espi: ep93xx SPI controller struct
  410. *
  411. * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If
  412. * called several times, the whole transfer will be completed. Returns
  413. * %-EINPROGRESS when current transfer was not yet completed otherwise %0.
  414. *
  415. * When this function is finished, RX FIFO should be empty and TX FIFO should be
  416. * full.
  417. */
  418. static int ep93xx_spi_read_write(struct ep93xx_spi *espi)
  419. {
  420. struct spi_message *msg = espi->current_msg;
  421. struct spi_transfer *t = msg->state;
  422. /* read as long as RX FIFO has frames in it */
  423. while ((ep93xx_spi_read_u8(espi, SSPSR) & SSPSR_RNE)) {
  424. ep93xx_do_read(espi, t);
  425. espi->fifo_level--;
  426. }
  427. /* write as long as TX FIFO has room */
  428. while (espi->fifo_level < SPI_FIFO_SIZE && espi->tx < t->len) {
  429. ep93xx_do_write(espi, t);
  430. espi->fifo_level++;
  431. }
  432. if (espi->rx == t->len) {
  433. msg->actual_length += t->len;
  434. return 0;
  435. }
  436. return -EINPROGRESS;
  437. }
  438. /**
  439. * ep93xx_spi_process_transfer() - processes one SPI transfer
  440. * @espi: ep93xx SPI controller struct
  441. * @msg: current message
  442. * @t: transfer to process
  443. *
  444. * This function processes one SPI transfer given in @t. Function waits until
  445. * transfer is complete (may sleep) and updates @msg->status based on whether
  446. * transfer was successfully processed or not.
  447. */
  448. static void ep93xx_spi_process_transfer(struct ep93xx_spi *espi,
  449. struct spi_message *msg,
  450. struct spi_transfer *t)
  451. {
  452. struct ep93xx_spi_chip *chip = spi_get_ctldata(msg->spi);
  453. msg->state = t;
  454. /*
  455. * Handle any transfer specific settings if needed. We use
  456. * temporary chip settings here and restore original later when
  457. * the transfer is finished.
  458. */
  459. if (t->speed_hz || t->bits_per_word) {
  460. struct ep93xx_spi_chip tmp_chip = *chip;
  461. if (t->speed_hz) {
  462. int err;
  463. err = ep93xx_spi_calc_divisors(espi, &tmp_chip,
  464. t->speed_hz);
  465. if (err) {
  466. dev_err(&espi->pdev->dev,
  467. "failed to adjust speed\n");
  468. msg->status = err;
  469. return;
  470. }
  471. }
  472. if (t->bits_per_word)
  473. tmp_chip.dss = bits_per_word_to_dss(t->bits_per_word);
  474. /*
  475. * Set up temporary new hw settings for this transfer.
  476. */
  477. ep93xx_spi_chip_setup(espi, &tmp_chip);
  478. }
  479. espi->rx = 0;
  480. espi->tx = 0;
  481. /*
  482. * Now everything is set up for the current transfer. We prime the TX
  483. * FIFO, enable interrupts, and wait for the transfer to complete.
  484. */
  485. if (ep93xx_spi_read_write(espi)) {
  486. ep93xx_spi_enable_interrupts(espi);
  487. wait_for_completion(&espi->wait);
  488. }
  489. /*
  490. * In case of error during transmit, we bail out from processing
  491. * the message.
  492. */
  493. if (msg->status)
  494. return;
  495. /*
  496. * After this transfer is finished, perform any possible
  497. * post-transfer actions requested by the protocol driver.
  498. */
  499. if (t->delay_usecs) {
  500. set_current_state(TASK_UNINTERRUPTIBLE);
  501. schedule_timeout(usecs_to_jiffies(t->delay_usecs));
  502. }
  503. if (t->cs_change) {
  504. if (!list_is_last(&t->transfer_list, &msg->transfers)) {
  505. /*
  506. * In case protocol driver is asking us to drop the
  507. * chipselect briefly, we let the scheduler to handle
  508. * any "delay" here.
  509. */
  510. ep93xx_spi_cs_control(msg->spi, false);
  511. cond_resched();
  512. ep93xx_spi_cs_control(msg->spi, true);
  513. }
  514. }
  515. if (t->speed_hz || t->bits_per_word)
  516. ep93xx_spi_chip_setup(espi, chip);
  517. }
  518. /*
  519. * ep93xx_spi_process_message() - process one SPI message
  520. * @espi: ep93xx SPI controller struct
  521. * @msg: message to process
  522. *
  523. * This function processes a single SPI message. We go through all transfers in
  524. * the message and pass them to ep93xx_spi_process_transfer(). Chipselect is
  525. * asserted during the whole message (unless per transfer cs_change is set).
  526. *
  527. * @msg->status contains %0 in case of success or negative error code in case of
  528. * failure.
  529. */
  530. static void ep93xx_spi_process_message(struct ep93xx_spi *espi,
  531. struct spi_message *msg)
  532. {
  533. unsigned long timeout;
  534. struct spi_transfer *t;
  535. int err;
  536. /*
  537. * Enable the SPI controller and its clock.
  538. */
  539. err = ep93xx_spi_enable(espi);
  540. if (err) {
  541. dev_err(&espi->pdev->dev, "failed to enable SPI controller\n");
  542. msg->status = err;
  543. return;
  544. }
  545. /*
  546. * Just to be sure: flush any data from RX FIFO.
  547. */
  548. timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT);
  549. while (ep93xx_spi_read_u16(espi, SSPSR) & SSPSR_RNE) {
  550. if (time_after(jiffies, timeout)) {
  551. dev_warn(&espi->pdev->dev,
  552. "timeout while flushing RX FIFO\n");
  553. msg->status = -ETIMEDOUT;
  554. return;
  555. }
  556. ep93xx_spi_read_u16(espi, SSPDR);
  557. }
  558. /*
  559. * We explicitly handle FIFO level. This way we don't have to check TX
  560. * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
  561. */
  562. espi->fifo_level = 0;
  563. /*
  564. * Update SPI controller registers according to spi device and assert
  565. * the chipselect.
  566. */
  567. ep93xx_spi_chip_setup(espi, spi_get_ctldata(msg->spi));
  568. ep93xx_spi_cs_control(msg->spi, true);
  569. list_for_each_entry(t, &msg->transfers, transfer_list) {
  570. ep93xx_spi_process_transfer(espi, msg, t);
  571. if (msg->status)
  572. break;
  573. }
  574. /*
  575. * Now the whole message is transferred (or failed for some reason). We
  576. * deselect the device and disable the SPI controller.
  577. */
  578. ep93xx_spi_cs_control(msg->spi, false);
  579. ep93xx_spi_disable(espi);
  580. }
  581. #define work_to_espi(work) (container_of((work), struct ep93xx_spi, msg_work))
  582. /**
  583. * ep93xx_spi_work() - EP93xx SPI workqueue worker function
  584. * @work: work struct
  585. *
  586. * Workqueue worker function. This function is called when there are new
  587. * SPI messages to be processed. Message is taken out from the queue and then
  588. * passed to ep93xx_spi_process_message().
  589. *
  590. * After message is transferred, protocol driver is notified by calling
  591. * @msg->complete(). In case of error, @msg->status is set to negative error
  592. * number, otherwise it contains zero (and @msg->actual_length is updated).
  593. */
  594. static void ep93xx_spi_work(struct work_struct *work)
  595. {
  596. struct ep93xx_spi *espi = work_to_espi(work);
  597. struct spi_message *msg;
  598. spin_lock_irq(&espi->lock);
  599. if (!espi->running || espi->current_msg ||
  600. list_empty(&espi->msg_queue)) {
  601. spin_unlock_irq(&espi->lock);
  602. return;
  603. }
  604. msg = list_first_entry(&espi->msg_queue, struct spi_message, queue);
  605. list_del_init(&msg->queue);
  606. espi->current_msg = msg;
  607. spin_unlock_irq(&espi->lock);
  608. ep93xx_spi_process_message(espi, msg);
  609. /*
  610. * Update the current message and re-schedule ourselves if there are
  611. * more messages in the queue.
  612. */
  613. spin_lock_irq(&espi->lock);
  614. espi->current_msg = NULL;
  615. if (espi->running && !list_empty(&espi->msg_queue))
  616. queue_work(espi->wq, &espi->msg_work);
  617. spin_unlock_irq(&espi->lock);
  618. /* notify the protocol driver that we are done with this message */
  619. msg->complete(msg->context);
  620. }
  621. static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id)
  622. {
  623. struct ep93xx_spi *espi = dev_id;
  624. u8 irq_status = ep93xx_spi_read_u8(espi, SSPIIR);
  625. /*
  626. * If we got ROR (receive overrun) interrupt we know that something is
  627. * wrong. Just abort the message.
  628. */
  629. if (unlikely(irq_status & SSPIIR_RORIS)) {
  630. /* clear the overrun interrupt */
  631. ep93xx_spi_write_u8(espi, SSPICR, 0);
  632. dev_warn(&espi->pdev->dev,
  633. "receive overrun, aborting the message\n");
  634. espi->current_msg->status = -EIO;
  635. } else {
  636. /*
  637. * Interrupt is either RX (RIS) or TX (TIS). For both cases we
  638. * simply execute next data transfer.
  639. */
  640. if (ep93xx_spi_read_write(espi)) {
  641. /*
  642. * In normal case, there still is some processing left
  643. * for current transfer. Let's wait for the next
  644. * interrupt then.
  645. */
  646. return IRQ_HANDLED;
  647. }
  648. }
  649. /*
  650. * Current transfer is finished, either with error or with success. In
  651. * any case we disable interrupts and notify the worker to handle
  652. * any post-processing of the message.
  653. */
  654. ep93xx_spi_disable_interrupts(espi);
  655. complete(&espi->wait);
  656. return IRQ_HANDLED;
  657. }
  658. static int __init ep93xx_spi_probe(struct platform_device *pdev)
  659. {
  660. struct spi_master *master;
  661. struct ep93xx_spi_info *info;
  662. struct ep93xx_spi *espi;
  663. struct resource *res;
  664. int error;
  665. info = pdev->dev.platform_data;
  666. master = spi_alloc_master(&pdev->dev, sizeof(*espi));
  667. if (!master) {
  668. dev_err(&pdev->dev, "failed to allocate spi master\n");
  669. return -ENOMEM;
  670. }
  671. master->setup = ep93xx_spi_setup;
  672. master->transfer = ep93xx_spi_transfer;
  673. master->cleanup = ep93xx_spi_cleanup;
  674. master->bus_num = pdev->id;
  675. master->num_chipselect = info->num_chipselect;
  676. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  677. platform_set_drvdata(pdev, master);
  678. espi = spi_master_get_devdata(master);
  679. espi->clk = clk_get(&pdev->dev, NULL);
  680. if (IS_ERR(espi->clk)) {
  681. dev_err(&pdev->dev, "unable to get spi clock\n");
  682. error = PTR_ERR(espi->clk);
  683. goto fail_release_master;
  684. }
  685. spin_lock_init(&espi->lock);
  686. init_completion(&espi->wait);
  687. /*
  688. * Calculate maximum and minimum supported clock rates
  689. * for the controller.
  690. */
  691. espi->max_rate = clk_get_rate(espi->clk) / 2;
  692. espi->min_rate = clk_get_rate(espi->clk) / (254 * 256);
  693. espi->pdev = pdev;
  694. espi->irq = platform_get_irq(pdev, 0);
  695. if (espi->irq < 0) {
  696. error = -EBUSY;
  697. dev_err(&pdev->dev, "failed to get irq resources\n");
  698. goto fail_put_clock;
  699. }
  700. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  701. if (!res) {
  702. dev_err(&pdev->dev, "unable to get iomem resource\n");
  703. error = -ENODEV;
  704. goto fail_put_clock;
  705. }
  706. res = request_mem_region(res->start, resource_size(res), pdev->name);
  707. if (!res) {
  708. dev_err(&pdev->dev, "unable to request iomem resources\n");
  709. error = -EBUSY;
  710. goto fail_put_clock;
  711. }
  712. espi->regs_base = ioremap(res->start, resource_size(res));
  713. if (!espi->regs_base) {
  714. dev_err(&pdev->dev, "failed to map resources\n");
  715. error = -ENODEV;
  716. goto fail_free_mem;
  717. }
  718. error = request_irq(espi->irq, ep93xx_spi_interrupt, 0,
  719. "ep93xx-spi", espi);
  720. if (error) {
  721. dev_err(&pdev->dev, "failed to request irq\n");
  722. goto fail_unmap_regs;
  723. }
  724. espi->wq = create_singlethread_workqueue("ep93xx_spid");
  725. if (!espi->wq) {
  726. dev_err(&pdev->dev, "unable to create workqueue\n");
  727. goto fail_free_irq;
  728. }
  729. INIT_WORK(&espi->msg_work, ep93xx_spi_work);
  730. INIT_LIST_HEAD(&espi->msg_queue);
  731. espi->running = true;
  732. /* make sure that the hardware is disabled */
  733. ep93xx_spi_write_u8(espi, SSPCR1, 0);
  734. error = spi_register_master(master);
  735. if (error) {
  736. dev_err(&pdev->dev, "failed to register SPI master\n");
  737. goto fail_free_queue;
  738. }
  739. dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n",
  740. (unsigned long)res->start, espi->irq);
  741. return 0;
  742. fail_free_queue:
  743. destroy_workqueue(espi->wq);
  744. fail_free_irq:
  745. free_irq(espi->irq, espi);
  746. fail_unmap_regs:
  747. iounmap(espi->regs_base);
  748. fail_free_mem:
  749. release_mem_region(res->start, resource_size(res));
  750. fail_put_clock:
  751. clk_put(espi->clk);
  752. fail_release_master:
  753. spi_master_put(master);
  754. platform_set_drvdata(pdev, NULL);
  755. return error;
  756. }
  757. static int __exit ep93xx_spi_remove(struct platform_device *pdev)
  758. {
  759. struct spi_master *master = platform_get_drvdata(pdev);
  760. struct ep93xx_spi *espi = spi_master_get_devdata(master);
  761. struct resource *res;
  762. spin_lock_irq(&espi->lock);
  763. espi->running = false;
  764. spin_unlock_irq(&espi->lock);
  765. destroy_workqueue(espi->wq);
  766. /*
  767. * Complete remaining messages with %-ESHUTDOWN status.
  768. */
  769. spin_lock_irq(&espi->lock);
  770. while (!list_empty(&espi->msg_queue)) {
  771. struct spi_message *msg;
  772. msg = list_first_entry(&espi->msg_queue,
  773. struct spi_message, queue);
  774. list_del_init(&msg->queue);
  775. msg->status = -ESHUTDOWN;
  776. spin_unlock_irq(&espi->lock);
  777. msg->complete(msg->context);
  778. spin_lock_irq(&espi->lock);
  779. }
  780. spin_unlock_irq(&espi->lock);
  781. free_irq(espi->irq, espi);
  782. iounmap(espi->regs_base);
  783. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  784. release_mem_region(res->start, resource_size(res));
  785. clk_put(espi->clk);
  786. platform_set_drvdata(pdev, NULL);
  787. spi_unregister_master(master);
  788. return 0;
  789. }
  790. static struct platform_driver ep93xx_spi_driver = {
  791. .driver = {
  792. .name = "ep93xx-spi",
  793. .owner = THIS_MODULE,
  794. },
  795. .remove = __exit_p(ep93xx_spi_remove),
  796. };
  797. static int __init ep93xx_spi_init(void)
  798. {
  799. return platform_driver_probe(&ep93xx_spi_driver, ep93xx_spi_probe);
  800. }
  801. module_init(ep93xx_spi_init);
  802. static void __exit ep93xx_spi_exit(void)
  803. {
  804. platform_driver_unregister(&ep93xx_spi_driver);
  805. }
  806. module_exit(ep93xx_spi_exit);
  807. MODULE_DESCRIPTION("EP93xx SPI Controller driver");
  808. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  809. MODULE_LICENSE("GPL");
  810. MODULE_ALIAS("platform:ep93xx-spi");