omap2_mcspi.c 34 KB

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