spi_mpc8xxx.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. /*
  2. * MPC8xxx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * CPM SPI and QE buffer descriptors mode support:
  9. * Copyright (c) 2009 MontaVista Software, Inc.
  10. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/errno.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/completion.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/device.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/spi_bitbang.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/fsl_devices.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mm.h>
  36. #include <linux/mutex.h>
  37. #include <linux/of.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/gpio.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/of_spi.h>
  42. #include <sysdev/fsl_soc.h>
  43. #include <asm/cpm.h>
  44. #include <asm/qe.h>
  45. #include <asm/irq.h>
  46. /* CPM1 and CPM2 are mutually exclusive. */
  47. #ifdef CONFIG_CPM1
  48. #include <asm/cpm1.h>
  49. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
  50. #else
  51. #include <asm/cpm2.h>
  52. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
  53. #endif
  54. /* SPI Controller registers */
  55. struct mpc8xxx_spi_reg {
  56. u8 res1[0x20];
  57. __be32 mode;
  58. __be32 event;
  59. __be32 mask;
  60. __be32 command;
  61. __be32 transmit;
  62. __be32 receive;
  63. };
  64. /* SPI Parameter RAM */
  65. struct spi_pram {
  66. __be16 rbase; /* Rx Buffer descriptor base address */
  67. __be16 tbase; /* Tx Buffer descriptor base address */
  68. u8 rfcr; /* Rx function code */
  69. u8 tfcr; /* Tx function code */
  70. __be16 mrblr; /* Max receive buffer length */
  71. __be32 rstate; /* Internal */
  72. __be32 rdp; /* Internal */
  73. __be16 rbptr; /* Internal */
  74. __be16 rbc; /* Internal */
  75. __be32 rxtmp; /* Internal */
  76. __be32 tstate; /* Internal */
  77. __be32 tdp; /* Internal */
  78. __be16 tbptr; /* Internal */
  79. __be16 tbc; /* Internal */
  80. __be32 txtmp; /* Internal */
  81. __be32 res; /* Tx temp. */
  82. __be16 rpbase; /* Relocation pointer (CPM1 only) */
  83. __be16 res1; /* Reserved */
  84. };
  85. /* SPI Controller mode register definitions */
  86. #define SPMODE_LOOP (1 << 30)
  87. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  88. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  89. #define SPMODE_DIV16 (1 << 27)
  90. #define SPMODE_REV (1 << 26)
  91. #define SPMODE_MS (1 << 25)
  92. #define SPMODE_ENABLE (1 << 24)
  93. #define SPMODE_LEN(x) ((x) << 20)
  94. #define SPMODE_PM(x) ((x) << 16)
  95. #define SPMODE_OP (1 << 14)
  96. #define SPMODE_CG(x) ((x) << 7)
  97. /*
  98. * Default for SPI Mode:
  99. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  100. */
  101. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  102. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  103. /* SPIE register values */
  104. #define SPIE_NE 0x00000200 /* Not empty */
  105. #define SPIE_NF 0x00000100 /* Not full */
  106. /* SPIM register values */
  107. #define SPIM_NE 0x00000200 /* Not empty */
  108. #define SPIM_NF 0x00000100 /* Not full */
  109. #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
  110. #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
  111. /* SPCOM register values */
  112. #define SPCOM_STR (1 << 23) /* Start transmit */
  113. #define SPI_PRAM_SIZE 0x100
  114. #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
  115. /* SPI Controller driver's private data. */
  116. struct mpc8xxx_spi {
  117. struct device *dev;
  118. struct mpc8xxx_spi_reg __iomem *base;
  119. /* rx & tx bufs from the spi_transfer */
  120. const void *tx;
  121. void *rx;
  122. int subblock;
  123. struct spi_pram __iomem *pram;
  124. struct cpm_buf_desc __iomem *tx_bd;
  125. struct cpm_buf_desc __iomem *rx_bd;
  126. struct spi_transfer *xfer_in_progress;
  127. /* dma addresses for CPM transfers */
  128. dma_addr_t tx_dma;
  129. dma_addr_t rx_dma;
  130. bool map_tx_dma;
  131. bool map_rx_dma;
  132. dma_addr_t dma_dummy_tx;
  133. dma_addr_t dma_dummy_rx;
  134. /* functions to deal with different sized buffers */
  135. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  136. u32(*get_tx) (struct mpc8xxx_spi *);
  137. unsigned int count;
  138. unsigned int irq;
  139. unsigned nsecs; /* (clock cycle time)/2 */
  140. u32 spibrg; /* SPIBRG input clock */
  141. u32 rx_shift; /* RX data reg shift when in qe mode */
  142. u32 tx_shift; /* TX data reg shift when in qe mode */
  143. unsigned int flags;
  144. #define SPI_QE_CPU_MODE (1 << 0) /* QE CPU ("PIO") mode */
  145. #define SPI_CPM_MODE (1 << 1) /* CPM/QE ("DMA") mode */
  146. #define SPI_CPM1 (1 << 2) /* SPI unit is in CPM1 block */
  147. #define SPI_CPM2 (1 << 3) /* SPI unit is in CPM2 block */
  148. #define SPI_QE (1 << 4) /* SPI unit is in QE block */
  149. struct workqueue_struct *workqueue;
  150. struct work_struct work;
  151. struct list_head queue;
  152. spinlock_t lock;
  153. struct completion done;
  154. };
  155. static void *mpc8xxx_dummy_rx;
  156. static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
  157. static int mpc8xxx_dummy_rx_refcnt;
  158. struct spi_mpc8xxx_cs {
  159. /* functions to deal with different sized buffers */
  160. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  161. u32 (*get_tx) (struct mpc8xxx_spi *);
  162. u32 rx_shift; /* RX data reg shift when in qe mode */
  163. u32 tx_shift; /* TX data reg shift when in qe mode */
  164. u32 hw_mode; /* Holds HW mode register settings */
  165. };
  166. static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
  167. {
  168. out_be32(reg, val);
  169. }
  170. static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
  171. {
  172. return in_be32(reg);
  173. }
  174. #define MPC83XX_SPI_RX_BUF(type) \
  175. static \
  176. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  177. { \
  178. type *rx = mpc8xxx_spi->rx; \
  179. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  180. mpc8xxx_spi->rx = rx; \
  181. }
  182. #define MPC83XX_SPI_TX_BUF(type) \
  183. static \
  184. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  185. { \
  186. u32 data; \
  187. const type *tx = mpc8xxx_spi->tx; \
  188. if (!tx) \
  189. return 0; \
  190. data = *tx++ << mpc8xxx_spi->tx_shift; \
  191. mpc8xxx_spi->tx = tx; \
  192. return data; \
  193. }
  194. MPC83XX_SPI_RX_BUF(u8)
  195. MPC83XX_SPI_RX_BUF(u16)
  196. MPC83XX_SPI_RX_BUF(u32)
  197. MPC83XX_SPI_TX_BUF(u8)
  198. MPC83XX_SPI_TX_BUF(u16)
  199. MPC83XX_SPI_TX_BUF(u32)
  200. static void mpc8xxx_spi_change_mode(struct spi_device *spi)
  201. {
  202. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  203. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  204. __be32 __iomem *mode = &mspi->base->mode;
  205. unsigned long flags;
  206. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  207. return;
  208. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  209. local_irq_save(flags);
  210. /* Turn off SPI unit prior changing mode */
  211. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  212. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  213. /* When in CPM mode, we need to reinit tx and rx. */
  214. if (mspi->flags & SPI_CPM_MODE) {
  215. if (mspi->flags & SPI_QE) {
  216. qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
  217. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  218. } else {
  219. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  220. if (mspi->flags & SPI_CPM1) {
  221. out_be16(&mspi->pram->rbptr,
  222. in_be16(&mspi->pram->rbase));
  223. out_be16(&mspi->pram->tbptr,
  224. in_be16(&mspi->pram->tbase));
  225. }
  226. }
  227. }
  228. local_irq_restore(flags);
  229. }
  230. static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
  231. {
  232. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  233. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  234. bool pol = spi->mode & SPI_CS_HIGH;
  235. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  236. if (value == BITBANG_CS_INACTIVE) {
  237. if (pdata->cs_control)
  238. pdata->cs_control(spi, !pol);
  239. }
  240. if (value == BITBANG_CS_ACTIVE) {
  241. mpc8xxx_spi->rx_shift = cs->rx_shift;
  242. mpc8xxx_spi->tx_shift = cs->tx_shift;
  243. mpc8xxx_spi->get_rx = cs->get_rx;
  244. mpc8xxx_spi->get_tx = cs->get_tx;
  245. mpc8xxx_spi_change_mode(spi);
  246. if (pdata->cs_control)
  247. pdata->cs_control(spi, pol);
  248. }
  249. }
  250. static
  251. int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  252. {
  253. struct mpc8xxx_spi *mpc8xxx_spi;
  254. u8 bits_per_word, pm;
  255. u32 hz;
  256. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  257. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  258. if (t) {
  259. bits_per_word = t->bits_per_word;
  260. hz = t->speed_hz;
  261. } else {
  262. bits_per_word = 0;
  263. hz = 0;
  264. }
  265. /* spi_transfer level calls that work per-word */
  266. if (!bits_per_word)
  267. bits_per_word = spi->bits_per_word;
  268. /* Make sure its a bit width we support [4..16, 32] */
  269. if ((bits_per_word < 4)
  270. || ((bits_per_word > 16) && (bits_per_word != 32)))
  271. return -EINVAL;
  272. if (!hz)
  273. hz = spi->max_speed_hz;
  274. cs->rx_shift = 0;
  275. cs->tx_shift = 0;
  276. if (bits_per_word <= 8) {
  277. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  278. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  279. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  280. cs->rx_shift = 16;
  281. cs->tx_shift = 24;
  282. }
  283. } else if (bits_per_word <= 16) {
  284. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  285. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  286. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  287. cs->rx_shift = 16;
  288. cs->tx_shift = 16;
  289. }
  290. } else if (bits_per_word <= 32) {
  291. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  292. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  293. } else
  294. return -EINVAL;
  295. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  296. spi->mode & SPI_LSB_FIRST) {
  297. cs->tx_shift = 0;
  298. if (bits_per_word <= 8)
  299. cs->rx_shift = 8;
  300. else
  301. cs->rx_shift = 0;
  302. }
  303. mpc8xxx_spi->rx_shift = cs->rx_shift;
  304. mpc8xxx_spi->tx_shift = cs->tx_shift;
  305. mpc8xxx_spi->get_rx = cs->get_rx;
  306. mpc8xxx_spi->get_tx = cs->get_tx;
  307. if (bits_per_word == 32)
  308. bits_per_word = 0;
  309. else
  310. bits_per_word = bits_per_word - 1;
  311. /* mask out bits we are going to set */
  312. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  313. | SPMODE_PM(0xF));
  314. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  315. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  316. cs->hw_mode |= SPMODE_DIV16;
  317. pm = mpc8xxx_spi->spibrg / (hz * 64);
  318. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  319. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  320. hz, mpc8xxx_spi->spibrg / 1024);
  321. if (pm > 16)
  322. pm = 16;
  323. } else
  324. pm = mpc8xxx_spi->spibrg / (hz * 4);
  325. if (pm)
  326. pm--;
  327. cs->hw_mode |= SPMODE_PM(pm);
  328. mpc8xxx_spi_change_mode(spi);
  329. return 0;
  330. }
  331. static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  332. {
  333. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  334. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  335. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  336. unsigned int xfer_ofs;
  337. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  338. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  339. out_be16(&rx_bd->cbd_datlen, 0);
  340. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  341. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  342. out_be16(&tx_bd->cbd_datlen, xfer_len);
  343. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  344. BD_SC_LAST);
  345. /* start transfer */
  346. mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
  347. }
  348. static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  349. struct spi_transfer *t, bool is_dma_mapped)
  350. {
  351. struct device *dev = mspi->dev;
  352. if (is_dma_mapped) {
  353. mspi->map_tx_dma = 0;
  354. mspi->map_rx_dma = 0;
  355. } else {
  356. mspi->map_tx_dma = 1;
  357. mspi->map_rx_dma = 1;
  358. }
  359. if (!t->tx_buf) {
  360. mspi->tx_dma = mspi->dma_dummy_tx;
  361. mspi->map_tx_dma = 0;
  362. }
  363. if (!t->rx_buf) {
  364. mspi->rx_dma = mspi->dma_dummy_rx;
  365. mspi->map_rx_dma = 0;
  366. }
  367. if (mspi->map_tx_dma) {
  368. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  369. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  370. DMA_TO_DEVICE);
  371. if (dma_mapping_error(dev, mspi->tx_dma)) {
  372. dev_err(dev, "unable to map tx dma\n");
  373. return -ENOMEM;
  374. }
  375. } else {
  376. mspi->tx_dma = t->tx_dma;
  377. }
  378. if (mspi->map_rx_dma) {
  379. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  380. DMA_FROM_DEVICE);
  381. if (dma_mapping_error(dev, mspi->rx_dma)) {
  382. dev_err(dev, "unable to map rx dma\n");
  383. goto err_rx_dma;
  384. }
  385. } else {
  386. mspi->rx_dma = t->rx_dma;
  387. }
  388. /* enable rx ints */
  389. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
  390. mspi->xfer_in_progress = t;
  391. mspi->count = t->len;
  392. /* start CPM transfers */
  393. mpc8xxx_spi_cpm_bufs_start(mspi);
  394. return 0;
  395. err_rx_dma:
  396. if (mspi->map_tx_dma)
  397. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  398. return -ENOMEM;
  399. }
  400. static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  401. {
  402. struct device *dev = mspi->dev;
  403. struct spi_transfer *t = mspi->xfer_in_progress;
  404. if (mspi->map_tx_dma)
  405. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  406. if (mspi->map_tx_dma)
  407. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  408. mspi->xfer_in_progress = NULL;
  409. }
  410. static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  411. struct spi_transfer *t, unsigned int len)
  412. {
  413. u32 word;
  414. mspi->count = len;
  415. /* enable rx ints */
  416. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
  417. /* transmit word */
  418. word = mspi->get_tx(mspi);
  419. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  420. return 0;
  421. }
  422. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  423. bool is_dma_mapped)
  424. {
  425. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  426. unsigned int len = t->len;
  427. u8 bits_per_word;
  428. int ret;
  429. bits_per_word = spi->bits_per_word;
  430. if (t->bits_per_word)
  431. bits_per_word = t->bits_per_word;
  432. if (bits_per_word > 8) {
  433. /* invalid length? */
  434. if (len & 1)
  435. return -EINVAL;
  436. len /= 2;
  437. }
  438. if (bits_per_word > 16) {
  439. /* invalid length? */
  440. if (len & 1)
  441. return -EINVAL;
  442. len /= 2;
  443. }
  444. mpc8xxx_spi->tx = t->tx_buf;
  445. mpc8xxx_spi->rx = t->rx_buf;
  446. INIT_COMPLETION(mpc8xxx_spi->done);
  447. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  448. ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  449. else
  450. ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
  451. if (ret)
  452. return ret;
  453. wait_for_completion(&mpc8xxx_spi->done);
  454. /* disable rx ints */
  455. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  456. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  457. mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
  458. return mpc8xxx_spi->count;
  459. }
  460. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  461. {
  462. struct spi_device *spi = m->spi;
  463. struct spi_transfer *t;
  464. unsigned int cs_change;
  465. const int nsecs = 50;
  466. int status;
  467. cs_change = 1;
  468. status = 0;
  469. list_for_each_entry(t, &m->transfers, transfer_list) {
  470. if (t->bits_per_word || t->speed_hz) {
  471. /* Don't allow changes if CS is active */
  472. status = -EINVAL;
  473. if (cs_change)
  474. status = mpc8xxx_spi_setup_transfer(spi, t);
  475. if (status < 0)
  476. break;
  477. }
  478. if (cs_change) {
  479. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  480. ndelay(nsecs);
  481. }
  482. cs_change = t->cs_change;
  483. if (t->len)
  484. status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
  485. if (status) {
  486. status = -EMSGSIZE;
  487. break;
  488. }
  489. m->actual_length += t->len;
  490. if (t->delay_usecs)
  491. udelay(t->delay_usecs);
  492. if (cs_change) {
  493. ndelay(nsecs);
  494. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  495. ndelay(nsecs);
  496. }
  497. }
  498. m->status = status;
  499. m->complete(m->context);
  500. if (status || !cs_change) {
  501. ndelay(nsecs);
  502. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  503. }
  504. mpc8xxx_spi_setup_transfer(spi, NULL);
  505. }
  506. static void mpc8xxx_spi_work(struct work_struct *work)
  507. {
  508. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  509. work);
  510. spin_lock_irq(&mpc8xxx_spi->lock);
  511. while (!list_empty(&mpc8xxx_spi->queue)) {
  512. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  513. struct spi_message, queue);
  514. list_del_init(&m->queue);
  515. spin_unlock_irq(&mpc8xxx_spi->lock);
  516. mpc8xxx_spi_do_one_msg(m);
  517. spin_lock_irq(&mpc8xxx_spi->lock);
  518. }
  519. spin_unlock_irq(&mpc8xxx_spi->lock);
  520. }
  521. static int mpc8xxx_spi_setup(struct spi_device *spi)
  522. {
  523. struct mpc8xxx_spi *mpc8xxx_spi;
  524. int retval;
  525. u32 hw_mode;
  526. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  527. if (!spi->max_speed_hz)
  528. return -EINVAL;
  529. if (!cs) {
  530. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  531. if (!cs)
  532. return -ENOMEM;
  533. spi->controller_state = cs;
  534. }
  535. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  536. hw_mode = cs->hw_mode; /* Save orginal settings */
  537. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  538. /* mask out bits we are going to set */
  539. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  540. | SPMODE_REV | SPMODE_LOOP);
  541. if (spi->mode & SPI_CPHA)
  542. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  543. if (spi->mode & SPI_CPOL)
  544. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  545. if (!(spi->mode & SPI_LSB_FIRST))
  546. cs->hw_mode |= SPMODE_REV;
  547. if (spi->mode & SPI_LOOP)
  548. cs->hw_mode |= SPMODE_LOOP;
  549. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  550. if (retval < 0) {
  551. cs->hw_mode = hw_mode; /* Restore settings */
  552. return retval;
  553. }
  554. return 0;
  555. }
  556. static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  557. {
  558. u16 len;
  559. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  560. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  561. len = in_be16(&mspi->rx_bd->cbd_datlen);
  562. if (len > mspi->count) {
  563. WARN_ON(1);
  564. len = mspi->count;
  565. }
  566. /* Clear the events */
  567. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  568. mspi->count -= len;
  569. if (mspi->count)
  570. mpc8xxx_spi_cpm_bufs_start(mspi);
  571. else
  572. complete(&mspi->done);
  573. }
  574. static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  575. {
  576. /* We need handle RX first */
  577. if (events & SPIE_NE) {
  578. u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
  579. if (mspi->rx)
  580. mspi->get_rx(rx_data, mspi);
  581. }
  582. if ((events & SPIE_NF) == 0)
  583. /* spin until TX is done */
  584. while (((events =
  585. mpc8xxx_spi_read_reg(&mspi->base->event)) &
  586. SPIE_NF) == 0)
  587. cpu_relax();
  588. /* Clear the events */
  589. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  590. mspi->count -= 1;
  591. if (mspi->count) {
  592. u32 word = mspi->get_tx(mspi);
  593. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  594. } else {
  595. complete(&mspi->done);
  596. }
  597. }
  598. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  599. {
  600. struct mpc8xxx_spi *mspi = context_data;
  601. irqreturn_t ret = IRQ_NONE;
  602. u32 events;
  603. /* Get interrupt events(tx/rx) */
  604. events = mpc8xxx_spi_read_reg(&mspi->base->event);
  605. if (events)
  606. ret = IRQ_HANDLED;
  607. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  608. if (mspi->flags & SPI_CPM_MODE)
  609. mpc8xxx_spi_cpm_irq(mspi, events);
  610. else
  611. mpc8xxx_spi_cpu_irq(mspi, events);
  612. return ret;
  613. }
  614. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  615. struct spi_message *m)
  616. {
  617. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  618. unsigned long flags;
  619. m->actual_length = 0;
  620. m->status = -EINPROGRESS;
  621. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  622. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  623. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  624. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  625. return 0;
  626. }
  627. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  628. {
  629. kfree(spi->controller_state);
  630. }
  631. static void *mpc8xxx_spi_alloc_dummy_rx(void)
  632. {
  633. mutex_lock(&mpc8xxx_dummy_rx_lock);
  634. if (!mpc8xxx_dummy_rx)
  635. mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  636. if (mpc8xxx_dummy_rx)
  637. mpc8xxx_dummy_rx_refcnt++;
  638. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  639. return mpc8xxx_dummy_rx;
  640. }
  641. static void mpc8xxx_spi_free_dummy_rx(void)
  642. {
  643. mutex_lock(&mpc8xxx_dummy_rx_lock);
  644. switch (mpc8xxx_dummy_rx_refcnt) {
  645. case 0:
  646. WARN_ON(1);
  647. break;
  648. case 1:
  649. kfree(mpc8xxx_dummy_rx);
  650. mpc8xxx_dummy_rx = NULL;
  651. /* fall through */
  652. default:
  653. mpc8xxx_dummy_rx_refcnt--;
  654. break;
  655. }
  656. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  657. }
  658. static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  659. {
  660. struct device *dev = mspi->dev;
  661. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  662. const u32 *iprop;
  663. int size;
  664. unsigned long spi_base_ofs;
  665. unsigned long pram_ofs = -ENOMEM;
  666. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  667. iprop = of_get_property(np, "reg", &size);
  668. /* QE with a fixed pram location? */
  669. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  670. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  671. /* QE but with a dynamic pram location? */
  672. if (mspi->flags & SPI_QE) {
  673. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  674. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  675. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  676. return pram_ofs;
  677. }
  678. /* CPM1 and CPM2 pram must be at a fixed addr. */
  679. if (!iprop || size != sizeof(*iprop) * 4)
  680. return -ENOMEM;
  681. spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
  682. if (IS_ERR_VALUE(spi_base_ofs))
  683. return -ENOMEM;
  684. if (mspi->flags & SPI_CPM2) {
  685. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  686. if (!IS_ERR_VALUE(pram_ofs)) {
  687. u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
  688. out_be16(spi_base, pram_ofs);
  689. }
  690. } else {
  691. struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
  692. u16 rpbase = in_be16(&pram->rpbase);
  693. /* Microcode relocation patch applied? */
  694. if (rpbase)
  695. pram_ofs = rpbase;
  696. else
  697. return spi_base_ofs;
  698. }
  699. cpm_muram_free(spi_base_ofs);
  700. return pram_ofs;
  701. }
  702. static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
  703. {
  704. struct device *dev = mspi->dev;
  705. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  706. const u32 *iprop;
  707. int size;
  708. unsigned long pram_ofs;
  709. unsigned long bds_ofs;
  710. if (!(mspi->flags & SPI_CPM_MODE))
  711. return 0;
  712. if (!mpc8xxx_spi_alloc_dummy_rx())
  713. return -ENOMEM;
  714. if (mspi->flags & SPI_QE) {
  715. iprop = of_get_property(np, "cell-index", &size);
  716. if (iprop && size == sizeof(*iprop))
  717. mspi->subblock = *iprop;
  718. switch (mspi->subblock) {
  719. default:
  720. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  721. /* fall through */
  722. case 0:
  723. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  724. break;
  725. case 1:
  726. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  727. break;
  728. }
  729. }
  730. pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
  731. if (IS_ERR_VALUE(pram_ofs)) {
  732. dev_err(dev, "can't allocate spi parameter ram\n");
  733. goto err_pram;
  734. }
  735. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  736. sizeof(*mspi->rx_bd), 8);
  737. if (IS_ERR_VALUE(bds_ofs)) {
  738. dev_err(dev, "can't allocate bds\n");
  739. goto err_bds;
  740. }
  741. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  742. DMA_TO_DEVICE);
  743. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  744. dev_err(dev, "unable to map dummy tx buffer\n");
  745. goto err_dummy_tx;
  746. }
  747. mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
  748. DMA_FROM_DEVICE);
  749. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  750. dev_err(dev, "unable to map dummy rx buffer\n");
  751. goto err_dummy_rx;
  752. }
  753. mspi->pram = cpm_muram_addr(pram_ofs);
  754. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  755. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  756. /* Initialize parameter ram. */
  757. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  758. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  759. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  760. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  761. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  762. out_be32(&mspi->pram->rstate, 0);
  763. out_be32(&mspi->pram->rdp, 0);
  764. out_be16(&mspi->pram->rbptr, 0);
  765. out_be16(&mspi->pram->rbc, 0);
  766. out_be32(&mspi->pram->rxtmp, 0);
  767. out_be32(&mspi->pram->tstate, 0);
  768. out_be32(&mspi->pram->tdp, 0);
  769. out_be16(&mspi->pram->tbptr, 0);
  770. out_be16(&mspi->pram->tbc, 0);
  771. out_be32(&mspi->pram->txtmp, 0);
  772. return 0;
  773. err_dummy_rx:
  774. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  775. err_dummy_tx:
  776. cpm_muram_free(bds_ofs);
  777. err_bds:
  778. cpm_muram_free(pram_ofs);
  779. err_pram:
  780. mpc8xxx_spi_free_dummy_rx();
  781. return -ENOMEM;
  782. }
  783. static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
  784. {
  785. struct device *dev = mspi->dev;
  786. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  787. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  788. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  789. cpm_muram_free(cpm_muram_offset(mspi->pram));
  790. mpc8xxx_spi_free_dummy_rx();
  791. }
  792. static const char *mpc8xxx_spi_strmode(unsigned int flags)
  793. {
  794. if (flags & SPI_QE_CPU_MODE) {
  795. return "QE CPU";
  796. } else if (flags & SPI_CPM_MODE) {
  797. if (flags & SPI_QE)
  798. return "QE";
  799. else if (flags & SPI_CPM2)
  800. return "CPM2";
  801. else
  802. return "CPM1";
  803. }
  804. return "CPU";
  805. }
  806. static struct spi_master * __devinit
  807. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  808. {
  809. struct fsl_spi_platform_data *pdata = dev->platform_data;
  810. struct spi_master *master;
  811. struct mpc8xxx_spi *mpc8xxx_spi;
  812. u32 regval;
  813. int ret = 0;
  814. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  815. if (master == NULL) {
  816. ret = -ENOMEM;
  817. goto err;
  818. }
  819. dev_set_drvdata(dev, master);
  820. /* the spi->mode bits understood by this driver: */
  821. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  822. | SPI_LSB_FIRST | SPI_LOOP;
  823. master->setup = mpc8xxx_spi_setup;
  824. master->transfer = mpc8xxx_spi_transfer;
  825. master->cleanup = mpc8xxx_spi_cleanup;
  826. mpc8xxx_spi = spi_master_get_devdata(master);
  827. mpc8xxx_spi->dev = dev;
  828. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  829. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  830. mpc8xxx_spi->flags = pdata->flags;
  831. mpc8xxx_spi->spibrg = pdata->sysclk;
  832. ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
  833. if (ret)
  834. goto err_cpm_init;
  835. mpc8xxx_spi->rx_shift = 0;
  836. mpc8xxx_spi->tx_shift = 0;
  837. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  838. mpc8xxx_spi->rx_shift = 16;
  839. mpc8xxx_spi->tx_shift = 24;
  840. }
  841. init_completion(&mpc8xxx_spi->done);
  842. mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
  843. if (mpc8xxx_spi->base == NULL) {
  844. ret = -ENOMEM;
  845. goto err_ioremap;
  846. }
  847. mpc8xxx_spi->irq = irq;
  848. /* Register for SPI Interrupt */
  849. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  850. 0, "mpc8xxx_spi", mpc8xxx_spi);
  851. if (ret != 0)
  852. goto unmap_io;
  853. master->bus_num = pdata->bus_num;
  854. master->num_chipselect = pdata->max_chipselect;
  855. /* SPI controller initializations */
  856. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  857. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  858. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  859. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  860. /* Enable SPI interface */
  861. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  862. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  863. regval |= SPMODE_OP;
  864. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  865. spin_lock_init(&mpc8xxx_spi->lock);
  866. init_completion(&mpc8xxx_spi->done);
  867. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  868. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  869. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  870. dev_name(master->dev.parent));
  871. if (mpc8xxx_spi->workqueue == NULL) {
  872. ret = -EBUSY;
  873. goto free_irq;
  874. }
  875. ret = spi_register_master(master);
  876. if (ret < 0)
  877. goto unreg_master;
  878. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
  879. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  880. return master;
  881. unreg_master:
  882. destroy_workqueue(mpc8xxx_spi->workqueue);
  883. free_irq:
  884. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  885. unmap_io:
  886. iounmap(mpc8xxx_spi->base);
  887. err_ioremap:
  888. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  889. err_cpm_init:
  890. spi_master_put(master);
  891. err:
  892. return ERR_PTR(ret);
  893. }
  894. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  895. {
  896. struct mpc8xxx_spi *mpc8xxx_spi;
  897. struct spi_master *master;
  898. master = dev_get_drvdata(dev);
  899. mpc8xxx_spi = spi_master_get_devdata(master);
  900. flush_workqueue(mpc8xxx_spi->workqueue);
  901. destroy_workqueue(mpc8xxx_spi->workqueue);
  902. spi_unregister_master(master);
  903. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  904. iounmap(mpc8xxx_spi->base);
  905. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  906. return 0;
  907. }
  908. struct mpc8xxx_spi_probe_info {
  909. struct fsl_spi_platform_data pdata;
  910. int *gpios;
  911. bool *alow_flags;
  912. };
  913. static struct mpc8xxx_spi_probe_info *
  914. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  915. {
  916. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  917. }
  918. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  919. {
  920. struct device *dev = spi->dev.parent;
  921. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  922. u16 cs = spi->chip_select;
  923. int gpio = pinfo->gpios[cs];
  924. bool alow = pinfo->alow_flags[cs];
  925. gpio_set_value(gpio, on ^ alow);
  926. }
  927. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  928. {
  929. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  930. struct fsl_spi_platform_data *pdata = dev->platform_data;
  931. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  932. unsigned int ngpios;
  933. int i = 0;
  934. int ret;
  935. ngpios = of_gpio_count(np);
  936. if (!ngpios) {
  937. /*
  938. * SPI w/o chip-select line. One SPI device is still permitted
  939. * though.
  940. */
  941. pdata->max_chipselect = 1;
  942. return 0;
  943. }
  944. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  945. if (!pinfo->gpios)
  946. return -ENOMEM;
  947. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  948. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  949. GFP_KERNEL);
  950. if (!pinfo->alow_flags) {
  951. ret = -ENOMEM;
  952. goto err_alloc_flags;
  953. }
  954. for (; i < ngpios; i++) {
  955. int gpio;
  956. enum of_gpio_flags flags;
  957. gpio = of_get_gpio_flags(np, i, &flags);
  958. if (!gpio_is_valid(gpio)) {
  959. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  960. ret = gpio;
  961. goto err_loop;
  962. }
  963. ret = gpio_request(gpio, dev_name(dev));
  964. if (ret) {
  965. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  966. goto err_loop;
  967. }
  968. pinfo->gpios[i] = gpio;
  969. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  970. ret = gpio_direction_output(pinfo->gpios[i],
  971. pinfo->alow_flags[i]);
  972. if (ret) {
  973. dev_err(dev, "can't set output direction for gpio "
  974. "#%d: %d\n", i, ret);
  975. goto err_loop;
  976. }
  977. }
  978. pdata->max_chipselect = ngpios;
  979. pdata->cs_control = mpc8xxx_spi_cs_control;
  980. return 0;
  981. err_loop:
  982. while (i >= 0) {
  983. if (gpio_is_valid(pinfo->gpios[i]))
  984. gpio_free(pinfo->gpios[i]);
  985. i--;
  986. }
  987. kfree(pinfo->alow_flags);
  988. pinfo->alow_flags = NULL;
  989. err_alloc_flags:
  990. kfree(pinfo->gpios);
  991. pinfo->gpios = NULL;
  992. return ret;
  993. }
  994. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  995. {
  996. struct fsl_spi_platform_data *pdata = dev->platform_data;
  997. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  998. int i;
  999. if (!pinfo->gpios)
  1000. return 0;
  1001. for (i = 0; i < pdata->max_chipselect; i++) {
  1002. if (gpio_is_valid(pinfo->gpios[i]))
  1003. gpio_free(pinfo->gpios[i]);
  1004. }
  1005. kfree(pinfo->gpios);
  1006. kfree(pinfo->alow_flags);
  1007. return 0;
  1008. }
  1009. static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
  1010. const struct of_device_id *ofid)
  1011. {
  1012. struct device *dev = &ofdev->dev;
  1013. struct device_node *np = ofdev->node;
  1014. struct mpc8xxx_spi_probe_info *pinfo;
  1015. struct fsl_spi_platform_data *pdata;
  1016. struct spi_master *master;
  1017. struct resource mem;
  1018. struct resource irq;
  1019. const void *prop;
  1020. int ret = -ENOMEM;
  1021. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  1022. if (!pinfo)
  1023. return -ENOMEM;
  1024. pdata = &pinfo->pdata;
  1025. dev->platform_data = pdata;
  1026. /* Allocate bus num dynamically. */
  1027. pdata->bus_num = -1;
  1028. /* SPI controller is either clocked from QE or SoC clock. */
  1029. pdata->sysclk = get_brgfreq();
  1030. if (pdata->sysclk == -1) {
  1031. pdata->sysclk = fsl_get_sys_freq();
  1032. if (pdata->sysclk == -1) {
  1033. ret = -ENODEV;
  1034. goto err_clk;
  1035. }
  1036. }
  1037. prop = of_get_property(np, "mode", NULL);
  1038. if (prop && !strcmp(prop, "cpu-qe"))
  1039. pdata->flags = SPI_QE_CPU_MODE;
  1040. else if (prop && !strcmp(prop, "qe"))
  1041. pdata->flags = SPI_CPM_MODE | SPI_QE;
  1042. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  1043. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  1044. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  1045. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  1046. ret = of_mpc8xxx_spi_get_chipselects(dev);
  1047. if (ret)
  1048. goto err;
  1049. ret = of_address_to_resource(np, 0, &mem);
  1050. if (ret)
  1051. goto err;
  1052. ret = of_irq_to_resource(np, 0, &irq);
  1053. if (!ret) {
  1054. ret = -EINVAL;
  1055. goto err;
  1056. }
  1057. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  1058. if (IS_ERR(master)) {
  1059. ret = PTR_ERR(master);
  1060. goto err;
  1061. }
  1062. of_register_spi_devices(master, np);
  1063. return 0;
  1064. err:
  1065. of_mpc8xxx_spi_free_chipselects(dev);
  1066. err_clk:
  1067. kfree(pinfo);
  1068. return ret;
  1069. }
  1070. static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
  1071. {
  1072. int ret;
  1073. ret = mpc8xxx_spi_remove(&ofdev->dev);
  1074. if (ret)
  1075. return ret;
  1076. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  1077. return 0;
  1078. }
  1079. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  1080. { .compatible = "fsl,spi" },
  1081. {},
  1082. };
  1083. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  1084. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  1085. .name = "mpc8xxx_spi",
  1086. .match_table = of_mpc8xxx_spi_match,
  1087. .probe = of_mpc8xxx_spi_probe,
  1088. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  1089. };
  1090. #ifdef CONFIG_MPC832x_RDB
  1091. /*
  1092. * XXX XXX XXX
  1093. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  1094. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  1095. * tree can work with OpenFirmware driver. But for now we support old trees
  1096. * as well.
  1097. */
  1098. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  1099. {
  1100. struct resource *mem;
  1101. unsigned int irq;
  1102. struct spi_master *master;
  1103. if (!pdev->dev.platform_data)
  1104. return -EINVAL;
  1105. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1106. if (!mem)
  1107. return -EINVAL;
  1108. irq = platform_get_irq(pdev, 0);
  1109. if (!irq)
  1110. return -EINVAL;
  1111. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  1112. if (IS_ERR(master))
  1113. return PTR_ERR(master);
  1114. return 0;
  1115. }
  1116. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  1117. {
  1118. return mpc8xxx_spi_remove(&pdev->dev);
  1119. }
  1120. MODULE_ALIAS("platform:mpc8xxx_spi");
  1121. static struct platform_driver mpc8xxx_spi_driver = {
  1122. .probe = plat_mpc8xxx_spi_probe,
  1123. .remove = __exit_p(plat_mpc8xxx_spi_remove),
  1124. .driver = {
  1125. .name = "mpc8xxx_spi",
  1126. .owner = THIS_MODULE,
  1127. },
  1128. };
  1129. static bool legacy_driver_failed;
  1130. static void __init legacy_driver_register(void)
  1131. {
  1132. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  1133. }
  1134. static void __exit legacy_driver_unregister(void)
  1135. {
  1136. if (legacy_driver_failed)
  1137. return;
  1138. platform_driver_unregister(&mpc8xxx_spi_driver);
  1139. }
  1140. #else
  1141. static void __init legacy_driver_register(void) {}
  1142. static void __exit legacy_driver_unregister(void) {}
  1143. #endif /* CONFIG_MPC832x_RDB */
  1144. static int __init mpc8xxx_spi_init(void)
  1145. {
  1146. legacy_driver_register();
  1147. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  1148. }
  1149. static void __exit mpc8xxx_spi_exit(void)
  1150. {
  1151. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  1152. legacy_driver_unregister();
  1153. }
  1154. module_init(mpc8xxx_spi_init);
  1155. module_exit(mpc8xxx_spi_exit);
  1156. MODULE_AUTHOR("Kumar Gala");
  1157. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  1158. MODULE_LICENSE("GPL");