omap2_mcspi.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/spi/spi.h>
  37. #include <plat/dma.h>
  38. #include <plat/clock.h>
  39. #include <plat/mcspi.h>
  40. #define OMAP2_MCSPI_MAX_FREQ 48000000
  41. /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
  42. #define OMAP2_MCSPI_MAX_CTRL 4
  43. #define OMAP2_MCSPI_REVISION 0x00
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. /* per-channel banks, 0x14 bytes each, first is: */
  51. #define OMAP2_MCSPI_CHCONF0 0x2c
  52. #define OMAP2_MCSPI_CHSTAT0 0x30
  53. #define OMAP2_MCSPI_CHCTRL0 0x34
  54. #define OMAP2_MCSPI_TX0 0x38
  55. #define OMAP2_MCSPI_RX0 0x3c
  56. /* per-register bitmasks: */
  57. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  58. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  59. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  60. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  61. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  62. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  63. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  64. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  65. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  66. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  67. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  68. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  69. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  70. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  71. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  72. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  73. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  74. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  75. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  76. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  77. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  78. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  79. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  80. /* We have 2 DMA channels per CS, one for RX and one for TX */
  81. struct omap2_mcspi_dma {
  82. int dma_tx_channel;
  83. int dma_rx_channel;
  84. int dma_tx_sync_dev;
  85. int dma_rx_sync_dev;
  86. struct completion dma_tx_completion;
  87. struct completion dma_rx_completion;
  88. };
  89. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  90. * cache operations; better heuristics consider wordsize and bitrate.
  91. */
  92. #define DMA_MIN_BYTES 160
  93. struct omap2_mcspi {
  94. struct work_struct work;
  95. /* lock protects queue and registers */
  96. spinlock_t lock;
  97. struct list_head msg_queue;
  98. struct spi_master *master;
  99. /* Virtual base address of the controller */
  100. void __iomem *base;
  101. unsigned long phys;
  102. /* SPI1 has 4 channels, while SPI2 has 2 */
  103. struct omap2_mcspi_dma *dma_channels;
  104. struct device *dev;
  105. };
  106. struct omap2_mcspi_cs {
  107. void __iomem *base;
  108. unsigned long phys;
  109. int word_len;
  110. struct list_head node;
  111. /* Context save and restore shadow register */
  112. u32 chconf0;
  113. };
  114. /* used for context save and restore, structure members to be updated whenever
  115. * corresponding registers are modified.
  116. */
  117. struct omap2_mcspi_regs {
  118. u32 modulctrl;
  119. u32 wakeupenable;
  120. struct list_head cs;
  121. };
  122. static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
  123. static struct workqueue_struct *omap2_mcspi_wq;
  124. #define MOD_REG_BIT(val, mask, set) do { \
  125. if (set) \
  126. val |= mask; \
  127. else \
  128. val &= ~mask; \
  129. } while (0)
  130. static inline void mcspi_write_reg(struct spi_master *master,
  131. int idx, u32 val)
  132. {
  133. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  134. __raw_writel(val, mcspi->base + idx);
  135. }
  136. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  137. {
  138. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  139. return __raw_readl(mcspi->base + idx);
  140. }
  141. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  142. int idx, u32 val)
  143. {
  144. struct omap2_mcspi_cs *cs = spi->controller_state;
  145. __raw_writel(val, cs->base + idx);
  146. }
  147. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  148. {
  149. struct omap2_mcspi_cs *cs = spi->controller_state;
  150. return __raw_readl(cs->base + idx);
  151. }
  152. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  153. {
  154. struct omap2_mcspi_cs *cs = spi->controller_state;
  155. return cs->chconf0;
  156. }
  157. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  158. {
  159. struct omap2_mcspi_cs *cs = spi->controller_state;
  160. cs->chconf0 = val;
  161. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  162. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  163. }
  164. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  165. int is_read, int enable)
  166. {
  167. u32 l, rw;
  168. l = mcspi_cached_chconf0(spi);
  169. if (is_read) /* 1 is read, 0 write */
  170. rw = OMAP2_MCSPI_CHCONF_DMAR;
  171. else
  172. rw = OMAP2_MCSPI_CHCONF_DMAW;
  173. MOD_REG_BIT(l, rw, enable);
  174. mcspi_write_chconf0(spi, l);
  175. }
  176. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  177. {
  178. u32 l;
  179. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  180. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  181. /* Flash post-writes */
  182. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  183. }
  184. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  185. {
  186. u32 l;
  187. l = mcspi_cached_chconf0(spi);
  188. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  189. mcspi_write_chconf0(spi, l);
  190. }
  191. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  192. {
  193. u32 l;
  194. /* setup when switching from (reset default) slave mode
  195. * to single-channel master mode
  196. */
  197. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  198. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  199. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  200. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  201. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  202. omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
  203. }
  204. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  205. {
  206. struct spi_master *spi_cntrl;
  207. struct omap2_mcspi_cs *cs;
  208. spi_cntrl = mcspi->master;
  209. /* McSPI: context restore */
  210. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
  211. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
  212. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
  213. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
  214. list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
  215. node)
  216. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  217. }
  218. static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
  219. {
  220. pm_runtime_put_sync(mcspi->dev);
  221. }
  222. static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
  223. {
  224. return pm_runtime_get_sync(mcspi->dev);
  225. }
  226. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  227. {
  228. unsigned long timeout;
  229. timeout = jiffies + msecs_to_jiffies(1000);
  230. while (!(__raw_readl(reg) & bit)) {
  231. if (time_after(jiffies, timeout))
  232. return -1;
  233. cpu_relax();
  234. }
  235. return 0;
  236. }
  237. static unsigned
  238. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  239. {
  240. struct omap2_mcspi *mcspi;
  241. struct omap2_mcspi_cs *cs = spi->controller_state;
  242. struct omap2_mcspi_dma *mcspi_dma;
  243. unsigned int count, c;
  244. unsigned long base, tx_reg, rx_reg;
  245. int word_len, data_type, element_count;
  246. int elements = 0;
  247. u32 l;
  248. u8 * rx;
  249. const u8 * tx;
  250. void __iomem *chstat_reg;
  251. mcspi = spi_master_get_devdata(spi->master);
  252. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  253. l = mcspi_cached_chconf0(spi);
  254. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  255. count = xfer->len;
  256. c = count;
  257. word_len = cs->word_len;
  258. base = cs->phys;
  259. tx_reg = base + OMAP2_MCSPI_TX0;
  260. rx_reg = base + OMAP2_MCSPI_RX0;
  261. rx = xfer->rx_buf;
  262. tx = xfer->tx_buf;
  263. if (word_len <= 8) {
  264. data_type = OMAP_DMA_DATA_TYPE_S8;
  265. element_count = count;
  266. } else if (word_len <= 16) {
  267. data_type = OMAP_DMA_DATA_TYPE_S16;
  268. element_count = count >> 1;
  269. } else /* word_len <= 32 */ {
  270. data_type = OMAP_DMA_DATA_TYPE_S32;
  271. element_count = count >> 2;
  272. }
  273. if (tx != NULL) {
  274. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  275. data_type, element_count, 1,
  276. OMAP_DMA_SYNC_ELEMENT,
  277. mcspi_dma->dma_tx_sync_dev, 0);
  278. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  279. OMAP_DMA_AMODE_CONSTANT,
  280. tx_reg, 0, 0);
  281. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  282. OMAP_DMA_AMODE_POST_INC,
  283. xfer->tx_dma, 0, 0);
  284. }
  285. if (rx != NULL) {
  286. elements = element_count - 1;
  287. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  288. elements--;
  289. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  290. data_type, elements, 1,
  291. OMAP_DMA_SYNC_ELEMENT,
  292. mcspi_dma->dma_rx_sync_dev, 1);
  293. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  294. OMAP_DMA_AMODE_CONSTANT,
  295. rx_reg, 0, 0);
  296. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  297. OMAP_DMA_AMODE_POST_INC,
  298. xfer->rx_dma, 0, 0);
  299. }
  300. if (tx != NULL) {
  301. omap_start_dma(mcspi_dma->dma_tx_channel);
  302. omap2_mcspi_set_dma_req(spi, 0, 1);
  303. }
  304. if (rx != NULL) {
  305. omap_start_dma(mcspi_dma->dma_rx_channel);
  306. omap2_mcspi_set_dma_req(spi, 1, 1);
  307. }
  308. if (tx != NULL) {
  309. wait_for_completion(&mcspi_dma->dma_tx_completion);
  310. dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
  311. /* for TX_ONLY mode, be sure all words have shifted out */
  312. if (rx == NULL) {
  313. if (mcspi_wait_for_reg_bit(chstat_reg,
  314. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  315. dev_err(&spi->dev, "TXS timed out\n");
  316. else if (mcspi_wait_for_reg_bit(chstat_reg,
  317. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  318. dev_err(&spi->dev, "EOT timed out\n");
  319. }
  320. }
  321. if (rx != NULL) {
  322. wait_for_completion(&mcspi_dma->dma_rx_completion);
  323. dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
  324. omap2_mcspi_set_enable(spi, 0);
  325. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  326. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  327. & OMAP2_MCSPI_CHSTAT_RXS)) {
  328. u32 w;
  329. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  330. if (word_len <= 8)
  331. ((u8 *)xfer->rx_buf)[elements++] = w;
  332. else if (word_len <= 16)
  333. ((u16 *)xfer->rx_buf)[elements++] = w;
  334. else /* word_len <= 32 */
  335. ((u32 *)xfer->rx_buf)[elements++] = w;
  336. } else {
  337. dev_err(&spi->dev,
  338. "DMA RX penultimate word empty");
  339. count -= (word_len <= 8) ? 2 :
  340. (word_len <= 16) ? 4 :
  341. /* word_len <= 32 */ 8;
  342. omap2_mcspi_set_enable(spi, 1);
  343. return count;
  344. }
  345. }
  346. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  347. & OMAP2_MCSPI_CHSTAT_RXS)) {
  348. u32 w;
  349. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  350. if (word_len <= 8)
  351. ((u8 *)xfer->rx_buf)[elements] = w;
  352. else if (word_len <= 16)
  353. ((u16 *)xfer->rx_buf)[elements] = w;
  354. else /* word_len <= 32 */
  355. ((u32 *)xfer->rx_buf)[elements] = w;
  356. } else {
  357. dev_err(&spi->dev, "DMA RX last word empty");
  358. count -= (word_len <= 8) ? 1 :
  359. (word_len <= 16) ? 2 :
  360. /* word_len <= 32 */ 4;
  361. }
  362. omap2_mcspi_set_enable(spi, 1);
  363. }
  364. return count;
  365. }
  366. static unsigned
  367. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  368. {
  369. struct omap2_mcspi *mcspi;
  370. struct omap2_mcspi_cs *cs = spi->controller_state;
  371. unsigned int count, c;
  372. u32 l;
  373. void __iomem *base = cs->base;
  374. void __iomem *tx_reg;
  375. void __iomem *rx_reg;
  376. void __iomem *chstat_reg;
  377. int word_len;
  378. mcspi = spi_master_get_devdata(spi->master);
  379. count = xfer->len;
  380. c = count;
  381. word_len = cs->word_len;
  382. l = mcspi_cached_chconf0(spi);
  383. /* We store the pre-calculated register addresses on stack to speed
  384. * up the transfer loop. */
  385. tx_reg = base + OMAP2_MCSPI_TX0;
  386. rx_reg = base + OMAP2_MCSPI_RX0;
  387. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  388. if (c < (word_len>>3))
  389. return 0;
  390. if (word_len <= 8) {
  391. u8 *rx;
  392. const u8 *tx;
  393. rx = xfer->rx_buf;
  394. tx = xfer->tx_buf;
  395. do {
  396. c -= 1;
  397. if (tx != NULL) {
  398. if (mcspi_wait_for_reg_bit(chstat_reg,
  399. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  400. dev_err(&spi->dev, "TXS timed out\n");
  401. goto out;
  402. }
  403. dev_vdbg(&spi->dev, "write-%d %02x\n",
  404. word_len, *tx);
  405. __raw_writel(*tx++, tx_reg);
  406. }
  407. if (rx != NULL) {
  408. if (mcspi_wait_for_reg_bit(chstat_reg,
  409. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  410. dev_err(&spi->dev, "RXS timed out\n");
  411. goto out;
  412. }
  413. if (c == 1 && tx == NULL &&
  414. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  415. omap2_mcspi_set_enable(spi, 0);
  416. *rx++ = __raw_readl(rx_reg);
  417. dev_vdbg(&spi->dev, "read-%d %02x\n",
  418. word_len, *(rx - 1));
  419. if (mcspi_wait_for_reg_bit(chstat_reg,
  420. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  421. dev_err(&spi->dev,
  422. "RXS timed out\n");
  423. goto out;
  424. }
  425. c = 0;
  426. } else if (c == 0 && tx == NULL) {
  427. omap2_mcspi_set_enable(spi, 0);
  428. }
  429. *rx++ = __raw_readl(rx_reg);
  430. dev_vdbg(&spi->dev, "read-%d %02x\n",
  431. word_len, *(rx - 1));
  432. }
  433. } while (c);
  434. } else if (word_len <= 16) {
  435. u16 *rx;
  436. const u16 *tx;
  437. rx = xfer->rx_buf;
  438. tx = xfer->tx_buf;
  439. do {
  440. c -= 2;
  441. if (tx != NULL) {
  442. if (mcspi_wait_for_reg_bit(chstat_reg,
  443. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  444. dev_err(&spi->dev, "TXS timed out\n");
  445. goto out;
  446. }
  447. dev_vdbg(&spi->dev, "write-%d %04x\n",
  448. word_len, *tx);
  449. __raw_writel(*tx++, tx_reg);
  450. }
  451. if (rx != NULL) {
  452. if (mcspi_wait_for_reg_bit(chstat_reg,
  453. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  454. dev_err(&spi->dev, "RXS timed out\n");
  455. goto out;
  456. }
  457. if (c == 2 && tx == NULL &&
  458. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  459. omap2_mcspi_set_enable(spi, 0);
  460. *rx++ = __raw_readl(rx_reg);
  461. dev_vdbg(&spi->dev, "read-%d %04x\n",
  462. word_len, *(rx - 1));
  463. if (mcspi_wait_for_reg_bit(chstat_reg,
  464. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  465. dev_err(&spi->dev,
  466. "RXS timed out\n");
  467. goto out;
  468. }
  469. c = 0;
  470. } else if (c == 0 && tx == NULL) {
  471. omap2_mcspi_set_enable(spi, 0);
  472. }
  473. *rx++ = __raw_readl(rx_reg);
  474. dev_vdbg(&spi->dev, "read-%d %04x\n",
  475. word_len, *(rx - 1));
  476. }
  477. } while (c >= 2);
  478. } else if (word_len <= 32) {
  479. u32 *rx;
  480. const u32 *tx;
  481. rx = xfer->rx_buf;
  482. tx = xfer->tx_buf;
  483. do {
  484. c -= 4;
  485. if (tx != NULL) {
  486. if (mcspi_wait_for_reg_bit(chstat_reg,
  487. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  488. dev_err(&spi->dev, "TXS timed out\n");
  489. goto out;
  490. }
  491. dev_vdbg(&spi->dev, "write-%d %08x\n",
  492. word_len, *tx);
  493. __raw_writel(*tx++, tx_reg);
  494. }
  495. if (rx != NULL) {
  496. if (mcspi_wait_for_reg_bit(chstat_reg,
  497. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  498. dev_err(&spi->dev, "RXS timed out\n");
  499. goto out;
  500. }
  501. if (c == 4 && tx == NULL &&
  502. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  503. omap2_mcspi_set_enable(spi, 0);
  504. *rx++ = __raw_readl(rx_reg);
  505. dev_vdbg(&spi->dev, "read-%d %08x\n",
  506. word_len, *(rx - 1));
  507. if (mcspi_wait_for_reg_bit(chstat_reg,
  508. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  509. dev_err(&spi->dev,
  510. "RXS timed out\n");
  511. goto out;
  512. }
  513. c = 0;
  514. } else if (c == 0 && tx == NULL) {
  515. omap2_mcspi_set_enable(spi, 0);
  516. }
  517. *rx++ = __raw_readl(rx_reg);
  518. dev_vdbg(&spi->dev, "read-%d %08x\n",
  519. word_len, *(rx - 1));
  520. }
  521. } while (c >= 4);
  522. }
  523. /* for TX_ONLY mode, be sure all words have shifted out */
  524. if (xfer->rx_buf == NULL) {
  525. if (mcspi_wait_for_reg_bit(chstat_reg,
  526. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  527. dev_err(&spi->dev, "TXS timed out\n");
  528. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  529. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  530. dev_err(&spi->dev, "EOT timed out\n");
  531. /* disable chan to purge rx datas received in TX_ONLY transfer,
  532. * otherwise these rx datas will affect the direct following
  533. * RX_ONLY transfer.
  534. */
  535. omap2_mcspi_set_enable(spi, 0);
  536. }
  537. out:
  538. omap2_mcspi_set_enable(spi, 1);
  539. return count - c;
  540. }
  541. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  542. {
  543. u32 div;
  544. for (div = 0; div < 15; div++)
  545. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  546. return div;
  547. return 15;
  548. }
  549. /* called only when no transfer is active to this device */
  550. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  551. struct spi_transfer *t)
  552. {
  553. struct omap2_mcspi_cs *cs = spi->controller_state;
  554. struct omap2_mcspi *mcspi;
  555. struct spi_master *spi_cntrl;
  556. u32 l = 0, div = 0;
  557. u8 word_len = spi->bits_per_word;
  558. u32 speed_hz = spi->max_speed_hz;
  559. mcspi = spi_master_get_devdata(spi->master);
  560. spi_cntrl = mcspi->master;
  561. if (t != NULL && t->bits_per_word)
  562. word_len = t->bits_per_word;
  563. cs->word_len = word_len;
  564. if (t && t->speed_hz)
  565. speed_hz = t->speed_hz;
  566. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  567. div = omap2_mcspi_calc_divisor(speed_hz);
  568. l = mcspi_cached_chconf0(spi);
  569. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  570. * REVISIT: this controller could support SPI_3WIRE mode.
  571. */
  572. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  573. l |= OMAP2_MCSPI_CHCONF_DPE0;
  574. /* wordlength */
  575. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  576. l |= (word_len - 1) << 7;
  577. /* set chipselect polarity; manage with FORCE */
  578. if (!(spi->mode & SPI_CS_HIGH))
  579. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  580. else
  581. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  582. /* set clock divisor */
  583. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  584. l |= div << 2;
  585. /* set SPI mode 0..3 */
  586. if (spi->mode & SPI_CPOL)
  587. l |= OMAP2_MCSPI_CHCONF_POL;
  588. else
  589. l &= ~OMAP2_MCSPI_CHCONF_POL;
  590. if (spi->mode & SPI_CPHA)
  591. l |= OMAP2_MCSPI_CHCONF_PHA;
  592. else
  593. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  594. mcspi_write_chconf0(spi, l);
  595. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  596. OMAP2_MCSPI_MAX_FREQ >> div,
  597. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  598. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  599. return 0;
  600. }
  601. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  602. {
  603. struct spi_device *spi = data;
  604. struct omap2_mcspi *mcspi;
  605. struct omap2_mcspi_dma *mcspi_dma;
  606. mcspi = spi_master_get_devdata(spi->master);
  607. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  608. complete(&mcspi_dma->dma_rx_completion);
  609. /* We must disable the DMA RX request */
  610. omap2_mcspi_set_dma_req(spi, 1, 0);
  611. }
  612. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  613. {
  614. struct spi_device *spi = data;
  615. struct omap2_mcspi *mcspi;
  616. struct omap2_mcspi_dma *mcspi_dma;
  617. mcspi = spi_master_get_devdata(spi->master);
  618. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  619. complete(&mcspi_dma->dma_tx_completion);
  620. /* We must disable the DMA TX request */
  621. omap2_mcspi_set_dma_req(spi, 0, 0);
  622. }
  623. static int omap2_mcspi_request_dma(struct spi_device *spi)
  624. {
  625. struct spi_master *master = spi->master;
  626. struct omap2_mcspi *mcspi;
  627. struct omap2_mcspi_dma *mcspi_dma;
  628. mcspi = spi_master_get_devdata(master);
  629. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  630. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  631. omap2_mcspi_dma_rx_callback, spi,
  632. &mcspi_dma->dma_rx_channel)) {
  633. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  634. return -EAGAIN;
  635. }
  636. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  637. omap2_mcspi_dma_tx_callback, spi,
  638. &mcspi_dma->dma_tx_channel)) {
  639. omap_free_dma(mcspi_dma->dma_rx_channel);
  640. mcspi_dma->dma_rx_channel = -1;
  641. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  642. return -EAGAIN;
  643. }
  644. init_completion(&mcspi_dma->dma_rx_completion);
  645. init_completion(&mcspi_dma->dma_tx_completion);
  646. return 0;
  647. }
  648. static int omap2_mcspi_setup(struct spi_device *spi)
  649. {
  650. int ret;
  651. struct omap2_mcspi *mcspi;
  652. struct omap2_mcspi_dma *mcspi_dma;
  653. struct omap2_mcspi_cs *cs = spi->controller_state;
  654. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  655. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  656. spi->bits_per_word);
  657. return -EINVAL;
  658. }
  659. mcspi = spi_master_get_devdata(spi->master);
  660. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  661. if (!cs) {
  662. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  663. if (!cs)
  664. return -ENOMEM;
  665. cs->base = mcspi->base + spi->chip_select * 0x14;
  666. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  667. cs->chconf0 = 0;
  668. spi->controller_state = cs;
  669. /* Link this to context save list */
  670. list_add_tail(&cs->node,
  671. &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
  672. }
  673. if (mcspi_dma->dma_rx_channel == -1
  674. || mcspi_dma->dma_tx_channel == -1) {
  675. ret = omap2_mcspi_request_dma(spi);
  676. if (ret < 0)
  677. return ret;
  678. }
  679. ret = omap2_mcspi_enable_clocks(mcspi);
  680. if (ret < 0)
  681. return ret;
  682. ret = omap2_mcspi_setup_transfer(spi, NULL);
  683. omap2_mcspi_disable_clocks(mcspi);
  684. return ret;
  685. }
  686. static void omap2_mcspi_cleanup(struct spi_device *spi)
  687. {
  688. struct omap2_mcspi *mcspi;
  689. struct omap2_mcspi_dma *mcspi_dma;
  690. struct omap2_mcspi_cs *cs;
  691. mcspi = spi_master_get_devdata(spi->master);
  692. if (spi->controller_state) {
  693. /* Unlink controller state from context save list */
  694. cs = spi->controller_state;
  695. list_del(&cs->node);
  696. kfree(spi->controller_state);
  697. }
  698. if (spi->chip_select < spi->master->num_chipselect) {
  699. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  700. if (mcspi_dma->dma_rx_channel != -1) {
  701. omap_free_dma(mcspi_dma->dma_rx_channel);
  702. mcspi_dma->dma_rx_channel = -1;
  703. }
  704. if (mcspi_dma->dma_tx_channel != -1) {
  705. omap_free_dma(mcspi_dma->dma_tx_channel);
  706. mcspi_dma->dma_tx_channel = -1;
  707. }
  708. }
  709. }
  710. static void omap2_mcspi_work(struct work_struct *work)
  711. {
  712. struct omap2_mcspi *mcspi;
  713. mcspi = container_of(work, struct omap2_mcspi, work);
  714. if (omap2_mcspi_enable_clocks(mcspi) < 0)
  715. return;
  716. spin_lock_irq(&mcspi->lock);
  717. /* We only enable one channel at a time -- the one whose message is
  718. * at the head of the queue -- although this controller would gladly
  719. * arbitrate among multiple channels. This corresponds to "single
  720. * channel" master mode. As a side effect, we need to manage the
  721. * chipselect with the FORCE bit ... CS != channel enable.
  722. */
  723. while (!list_empty(&mcspi->msg_queue)) {
  724. struct spi_message *m;
  725. struct spi_device *spi;
  726. struct spi_transfer *t = NULL;
  727. int cs_active = 0;
  728. struct omap2_mcspi_cs *cs;
  729. struct omap2_mcspi_device_config *cd;
  730. int par_override = 0;
  731. int status = 0;
  732. u32 chconf;
  733. m = container_of(mcspi->msg_queue.next, struct spi_message,
  734. queue);
  735. list_del_init(&m->queue);
  736. spin_unlock_irq(&mcspi->lock);
  737. spi = m->spi;
  738. cs = spi->controller_state;
  739. cd = spi->controller_data;
  740. omap2_mcspi_set_enable(spi, 1);
  741. list_for_each_entry(t, &m->transfers, transfer_list) {
  742. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  743. status = -EINVAL;
  744. break;
  745. }
  746. if (par_override || t->speed_hz || t->bits_per_word) {
  747. par_override = 1;
  748. status = omap2_mcspi_setup_transfer(spi, t);
  749. if (status < 0)
  750. break;
  751. if (!t->speed_hz && !t->bits_per_word)
  752. par_override = 0;
  753. }
  754. if (!cs_active) {
  755. omap2_mcspi_force_cs(spi, 1);
  756. cs_active = 1;
  757. }
  758. chconf = mcspi_cached_chconf0(spi);
  759. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  760. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  761. if (t->tx_buf == NULL)
  762. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  763. else if (t->rx_buf == NULL)
  764. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  765. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  766. /* Turbo mode is for more than one word */
  767. if (t->len > ((cs->word_len + 7) >> 3))
  768. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  769. }
  770. mcspi_write_chconf0(spi, chconf);
  771. if (t->len) {
  772. unsigned count;
  773. /* RX_ONLY mode needs dummy data in TX reg */
  774. if (t->tx_buf == NULL)
  775. __raw_writel(0, cs->base
  776. + OMAP2_MCSPI_TX0);
  777. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  778. count = omap2_mcspi_txrx_dma(spi, t);
  779. else
  780. count = omap2_mcspi_txrx_pio(spi, t);
  781. m->actual_length += count;
  782. if (count != t->len) {
  783. status = -EIO;
  784. break;
  785. }
  786. }
  787. if (t->delay_usecs)
  788. udelay(t->delay_usecs);
  789. /* ignore the "leave it on after last xfer" hint */
  790. if (t->cs_change) {
  791. omap2_mcspi_force_cs(spi, 0);
  792. cs_active = 0;
  793. }
  794. }
  795. /* Restore defaults if they were overriden */
  796. if (par_override) {
  797. par_override = 0;
  798. status = omap2_mcspi_setup_transfer(spi, NULL);
  799. }
  800. if (cs_active)
  801. omap2_mcspi_force_cs(spi, 0);
  802. omap2_mcspi_set_enable(spi, 0);
  803. m->status = status;
  804. m->complete(m->context);
  805. spin_lock_irq(&mcspi->lock);
  806. }
  807. spin_unlock_irq(&mcspi->lock);
  808. omap2_mcspi_disable_clocks(mcspi);
  809. }
  810. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  811. {
  812. struct omap2_mcspi *mcspi;
  813. unsigned long flags;
  814. struct spi_transfer *t;
  815. m->actual_length = 0;
  816. m->status = 0;
  817. /* reject invalid messages and transfers */
  818. if (list_empty(&m->transfers) || !m->complete)
  819. return -EINVAL;
  820. list_for_each_entry(t, &m->transfers, transfer_list) {
  821. const void *tx_buf = t->tx_buf;
  822. void *rx_buf = t->rx_buf;
  823. unsigned len = t->len;
  824. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  825. || (len && !(rx_buf || tx_buf))
  826. || (t->bits_per_word &&
  827. ( t->bits_per_word < 4
  828. || t->bits_per_word > 32))) {
  829. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  830. t->speed_hz,
  831. len,
  832. tx_buf ? "tx" : "",
  833. rx_buf ? "rx" : "",
  834. t->bits_per_word);
  835. return -EINVAL;
  836. }
  837. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  838. dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n",
  839. t->speed_hz,
  840. OMAP2_MCSPI_MAX_FREQ >> 15);
  841. return -EINVAL;
  842. }
  843. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  844. continue;
  845. if (tx_buf != NULL) {
  846. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  847. len, DMA_TO_DEVICE);
  848. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  849. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  850. 'T', len);
  851. return -EINVAL;
  852. }
  853. }
  854. if (rx_buf != NULL) {
  855. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  856. DMA_FROM_DEVICE);
  857. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  858. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  859. 'R', len);
  860. if (tx_buf != NULL)
  861. dma_unmap_single(&spi->dev, t->tx_dma,
  862. len, DMA_TO_DEVICE);
  863. return -EINVAL;
  864. }
  865. }
  866. }
  867. mcspi = spi_master_get_devdata(spi->master);
  868. spin_lock_irqsave(&mcspi->lock, flags);
  869. list_add_tail(&m->queue, &mcspi->msg_queue);
  870. queue_work(omap2_mcspi_wq, &mcspi->work);
  871. spin_unlock_irqrestore(&mcspi->lock, flags);
  872. return 0;
  873. }
  874. static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  875. {
  876. struct spi_master *master = mcspi->master;
  877. u32 tmp;
  878. int ret = 0;
  879. ret = omap2_mcspi_enable_clocks(mcspi);
  880. if (ret < 0)
  881. return ret;
  882. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  883. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  884. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  885. omap2_mcspi_set_master_mode(master);
  886. omap2_mcspi_disable_clocks(mcspi);
  887. return 0;
  888. }
  889. static int omap_mcspi_runtime_resume(struct device *dev)
  890. {
  891. struct omap2_mcspi *mcspi;
  892. struct spi_master *master;
  893. master = dev_get_drvdata(dev);
  894. mcspi = spi_master_get_devdata(master);
  895. omap2_mcspi_restore_ctx(mcspi);
  896. return 0;
  897. }
  898. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  899. {
  900. struct spi_master *master;
  901. struct omap2_mcspi_platform_config *pdata = pdev->dev.platform_data;
  902. struct omap2_mcspi *mcspi;
  903. struct resource *r;
  904. int status = 0, i;
  905. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  906. if (master == NULL) {
  907. dev_dbg(&pdev->dev, "master allocation failed\n");
  908. return -ENOMEM;
  909. }
  910. /* the spi->mode bits understood by this driver: */
  911. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  912. if (pdev->id != -1)
  913. master->bus_num = pdev->id;
  914. master->setup = omap2_mcspi_setup;
  915. master->transfer = omap2_mcspi_transfer;
  916. master->cleanup = omap2_mcspi_cleanup;
  917. master->num_chipselect = pdata->num_cs;
  918. dev_set_drvdata(&pdev->dev, master);
  919. mcspi = spi_master_get_devdata(master);
  920. mcspi->master = master;
  921. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  922. if (r == NULL) {
  923. status = -ENODEV;
  924. goto err1;
  925. }
  926. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  927. dev_name(&pdev->dev))) {
  928. status = -EBUSY;
  929. goto err1;
  930. }
  931. r->start += pdata->regs_offset;
  932. r->end += pdata->regs_offset;
  933. mcspi->phys = r->start;
  934. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  935. if (!mcspi->base) {
  936. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  937. status = -ENOMEM;
  938. goto err2;
  939. }
  940. mcspi->dev = &pdev->dev;
  941. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  942. spin_lock_init(&mcspi->lock);
  943. INIT_LIST_HEAD(&mcspi->msg_queue);
  944. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  945. mcspi->dma_channels = kcalloc(master->num_chipselect,
  946. sizeof(struct omap2_mcspi_dma),
  947. GFP_KERNEL);
  948. if (mcspi->dma_channels == NULL)
  949. goto err2;
  950. for (i = 0; i < master->num_chipselect; i++) {
  951. char dma_ch_name[14];
  952. struct resource *dma_res;
  953. sprintf(dma_ch_name, "rx%d", i);
  954. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  955. dma_ch_name);
  956. if (!dma_res) {
  957. dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
  958. status = -ENODEV;
  959. break;
  960. }
  961. mcspi->dma_channels[i].dma_rx_channel = -1;
  962. mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
  963. sprintf(dma_ch_name, "tx%d", i);
  964. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  965. dma_ch_name);
  966. if (!dma_res) {
  967. dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
  968. status = -ENODEV;
  969. break;
  970. }
  971. mcspi->dma_channels[i].dma_tx_channel = -1;
  972. mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
  973. }
  974. pm_runtime_enable(&pdev->dev);
  975. if (status || omap2_mcspi_master_setup(mcspi) < 0)
  976. goto err3;
  977. status = spi_register_master(master);
  978. if (status < 0)
  979. goto err4;
  980. return status;
  981. err4:
  982. spi_master_put(master);
  983. err3:
  984. kfree(mcspi->dma_channels);
  985. err2:
  986. release_mem_region(r->start, (r->end - r->start) + 1);
  987. iounmap(mcspi->base);
  988. err1:
  989. return status;
  990. }
  991. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  992. {
  993. struct spi_master *master;
  994. struct omap2_mcspi *mcspi;
  995. struct omap2_mcspi_dma *dma_channels;
  996. struct resource *r;
  997. void __iomem *base;
  998. master = dev_get_drvdata(&pdev->dev);
  999. mcspi = spi_master_get_devdata(master);
  1000. dma_channels = mcspi->dma_channels;
  1001. omap2_mcspi_disable_clocks(mcspi);
  1002. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1003. release_mem_region(r->start, (r->end - r->start) + 1);
  1004. base = mcspi->base;
  1005. spi_unregister_master(master);
  1006. iounmap(base);
  1007. kfree(dma_channels);
  1008. return 0;
  1009. }
  1010. /* work with hotplug and coldplug */
  1011. MODULE_ALIAS("platform:omap2_mcspi");
  1012. #ifdef CONFIG_SUSPEND
  1013. /*
  1014. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1015. * unactive state when driver was suspend, then force it to unactive state at
  1016. * wake up.
  1017. */
  1018. static int omap2_mcspi_resume(struct device *dev)
  1019. {
  1020. struct spi_master *master = dev_get_drvdata(dev);
  1021. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1022. struct omap2_mcspi_cs *cs;
  1023. omap2_mcspi_enable_clocks(mcspi);
  1024. list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
  1025. node) {
  1026. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1027. /*
  1028. * We need to toggle CS state for OMAP take this
  1029. * change in account.
  1030. */
  1031. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
  1032. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1033. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
  1034. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1035. }
  1036. }
  1037. omap2_mcspi_disable_clocks(mcspi);
  1038. return 0;
  1039. }
  1040. #else
  1041. #define omap2_mcspi_resume NULL
  1042. #endif
  1043. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1044. .resume = omap2_mcspi_resume,
  1045. .runtime_resume = omap_mcspi_runtime_resume,
  1046. };
  1047. static struct platform_driver omap2_mcspi_driver = {
  1048. .driver = {
  1049. .name = "omap2_mcspi",
  1050. .owner = THIS_MODULE,
  1051. .pm = &omap2_mcspi_pm_ops
  1052. },
  1053. .remove = __exit_p(omap2_mcspi_remove),
  1054. };
  1055. static int __init omap2_mcspi_init(void)
  1056. {
  1057. omap2_mcspi_wq = create_singlethread_workqueue(
  1058. omap2_mcspi_driver.driver.name);
  1059. if (omap2_mcspi_wq == NULL)
  1060. return -1;
  1061. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1062. }
  1063. subsys_initcall(omap2_mcspi_init);
  1064. static void __exit omap2_mcspi_exit(void)
  1065. {
  1066. platform_driver_unregister(&omap2_mcspi_driver);
  1067. destroy_workqueue(omap2_mcspi_wq);
  1068. }
  1069. module_exit(omap2_mcspi_exit);
  1070. MODULE_LICENSE("GPL");