spi-omap2-mcspi.c 32 KB

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