omap2_mcspi.c 27 KB

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