spi-omap2-mcspi.c 33 KB

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