spi-omap2-mcspi.c 33 KB

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