spi-pxa2xx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/ioport.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/pci.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spi/pxa2xx_spi.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/delay.h>
  28. #include <linux/gpio.h>
  29. #include <linux/slab.h>
  30. #include <linux/clk.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/acpi.h>
  33. #include "spi-pxa2xx.h"
  34. MODULE_AUTHOR("Stephen Street");
  35. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  36. MODULE_LICENSE("GPL");
  37. MODULE_ALIAS("platform:pxa2xx-spi");
  38. #define TIMOUT_DFLT 1000
  39. /*
  40. * for testing SSCR1 changes that require SSP restart, basically
  41. * everything except the service and interrupt enables, the pxa270 developer
  42. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  43. * list, but the PXA255 dev man says all bits without really meaning the
  44. * service and interrupt enables
  45. */
  46. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  47. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  48. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  49. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  50. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  51. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  52. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  53. | QUARK_X1000_SSCR1_EFWR \
  54. | QUARK_X1000_SSCR1_RFT \
  55. | QUARK_X1000_SSCR1_TFT \
  56. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  57. #define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  58. #define SPI_CS_CONTROL_SW_MODE BIT(0)
  59. #define SPI_CS_CONTROL_CS_HIGH BIT(1)
  60. struct lpss_config {
  61. /* LPSS offset from drv_data->ioaddr */
  62. unsigned offset;
  63. /* Register offsets from drv_data->lpss_base or -1 */
  64. int reg_general;
  65. int reg_ssp;
  66. int reg_cs_ctrl;
  67. /* FIFO thresholds */
  68. u32 rx_threshold;
  69. u32 tx_threshold_lo;
  70. u32 tx_threshold_hi;
  71. };
  72. /* Keep these sorted with enum pxa_ssp_type */
  73. static const struct lpss_config lpss_platforms[] = {
  74. { /* LPSS_LPT_SSP */
  75. .offset = 0x800,
  76. .reg_general = 0x08,
  77. .reg_ssp = 0x0c,
  78. .reg_cs_ctrl = 0x18,
  79. .rx_threshold = 64,
  80. .tx_threshold_lo = 160,
  81. .tx_threshold_hi = 224,
  82. },
  83. { /* LPSS_BYT_SSP */
  84. .offset = 0x400,
  85. .reg_general = 0x08,
  86. .reg_ssp = 0x0c,
  87. .reg_cs_ctrl = 0x18,
  88. .rx_threshold = 64,
  89. .tx_threshold_lo = 160,
  90. .tx_threshold_hi = 224,
  91. },
  92. { /* LPSS_SPT_SSP */
  93. .offset = 0x200,
  94. .reg_general = -1,
  95. .reg_ssp = 0x20,
  96. .reg_cs_ctrl = 0x24,
  97. .rx_threshold = 1,
  98. .tx_threshold_lo = 32,
  99. .tx_threshold_hi = 56,
  100. },
  101. };
  102. static inline const struct lpss_config
  103. *lpss_get_config(const struct driver_data *drv_data)
  104. {
  105. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  106. }
  107. static bool is_lpss_ssp(const struct driver_data *drv_data)
  108. {
  109. switch (drv_data->ssp_type) {
  110. case LPSS_LPT_SSP:
  111. case LPSS_BYT_SSP:
  112. case LPSS_SPT_SSP:
  113. return true;
  114. default:
  115. return false;
  116. }
  117. }
  118. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  119. {
  120. return drv_data->ssp_type == QUARK_X1000_SSP;
  121. }
  122. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  123. {
  124. switch (drv_data->ssp_type) {
  125. case QUARK_X1000_SSP:
  126. return QUARK_X1000_SSCR1_CHANGE_MASK;
  127. default:
  128. return SSCR1_CHANGE_MASK;
  129. }
  130. }
  131. static u32
  132. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  133. {
  134. switch (drv_data->ssp_type) {
  135. case QUARK_X1000_SSP:
  136. return RX_THRESH_QUARK_X1000_DFLT;
  137. default:
  138. return RX_THRESH_DFLT;
  139. }
  140. }
  141. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  142. {
  143. u32 mask;
  144. switch (drv_data->ssp_type) {
  145. case QUARK_X1000_SSP:
  146. mask = QUARK_X1000_SSSR_TFL_MASK;
  147. break;
  148. default:
  149. mask = SSSR_TFL_MASK;
  150. break;
  151. }
  152. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  153. }
  154. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  155. u32 *sccr1_reg)
  156. {
  157. u32 mask;
  158. switch (drv_data->ssp_type) {
  159. case QUARK_X1000_SSP:
  160. mask = QUARK_X1000_SSCR1_RFT;
  161. break;
  162. default:
  163. mask = SSCR1_RFT;
  164. break;
  165. }
  166. *sccr1_reg &= ~mask;
  167. }
  168. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  169. u32 *sccr1_reg, u32 threshold)
  170. {
  171. switch (drv_data->ssp_type) {
  172. case QUARK_X1000_SSP:
  173. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  174. break;
  175. default:
  176. *sccr1_reg |= SSCR1_RxTresh(threshold);
  177. break;
  178. }
  179. }
  180. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  181. u32 clk_div, u8 bits)
  182. {
  183. switch (drv_data->ssp_type) {
  184. case QUARK_X1000_SSP:
  185. return clk_div
  186. | QUARK_X1000_SSCR0_Motorola
  187. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  188. | SSCR0_SSE;
  189. default:
  190. return clk_div
  191. | SSCR0_Motorola
  192. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  193. | SSCR0_SSE
  194. | (bits > 16 ? SSCR0_EDSS : 0);
  195. }
  196. }
  197. /*
  198. * Read and write LPSS SSP private registers. Caller must first check that
  199. * is_lpss_ssp() returns true before these can be called.
  200. */
  201. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  202. {
  203. WARN_ON(!drv_data->lpss_base);
  204. return readl(drv_data->lpss_base + offset);
  205. }
  206. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  207. unsigned offset, u32 value)
  208. {
  209. WARN_ON(!drv_data->lpss_base);
  210. writel(value, drv_data->lpss_base + offset);
  211. }
  212. /*
  213. * lpss_ssp_setup - perform LPSS SSP specific setup
  214. * @drv_data: pointer to the driver private data
  215. *
  216. * Perform LPSS SSP specific setup. This function must be called first if
  217. * one is going to use LPSS SSP private registers.
  218. */
  219. static void lpss_ssp_setup(struct driver_data *drv_data)
  220. {
  221. const struct lpss_config *config;
  222. u32 value;
  223. config = lpss_get_config(drv_data);
  224. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  225. /* Enable software chip select control */
  226. value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
  227. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  228. /* Enable multiblock DMA transfers */
  229. if (drv_data->master_info->enable_dma) {
  230. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  231. if (config->reg_general >= 0) {
  232. value = __lpss_ssp_read_priv(drv_data,
  233. config->reg_general);
  234. value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  235. __lpss_ssp_write_priv(drv_data,
  236. config->reg_general, value);
  237. }
  238. }
  239. }
  240. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  241. {
  242. const struct lpss_config *config;
  243. u32 value;
  244. config = lpss_get_config(drv_data);
  245. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  246. if (enable)
  247. value &= ~SPI_CS_CONTROL_CS_HIGH;
  248. else
  249. value |= SPI_CS_CONTROL_CS_HIGH;
  250. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  251. }
  252. static void cs_assert(struct driver_data *drv_data)
  253. {
  254. struct chip_data *chip = drv_data->cur_chip;
  255. if (drv_data->ssp_type == CE4100_SSP) {
  256. pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
  257. return;
  258. }
  259. if (chip->cs_control) {
  260. chip->cs_control(PXA2XX_CS_ASSERT);
  261. return;
  262. }
  263. if (gpio_is_valid(chip->gpio_cs)) {
  264. gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
  265. return;
  266. }
  267. if (is_lpss_ssp(drv_data))
  268. lpss_ssp_cs_control(drv_data, true);
  269. }
  270. static void cs_deassert(struct driver_data *drv_data)
  271. {
  272. struct chip_data *chip = drv_data->cur_chip;
  273. if (drv_data->ssp_type == CE4100_SSP)
  274. return;
  275. if (chip->cs_control) {
  276. chip->cs_control(PXA2XX_CS_DEASSERT);
  277. return;
  278. }
  279. if (gpio_is_valid(chip->gpio_cs)) {
  280. gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
  281. return;
  282. }
  283. if (is_lpss_ssp(drv_data))
  284. lpss_ssp_cs_control(drv_data, false);
  285. }
  286. int pxa2xx_spi_flush(struct driver_data *drv_data)
  287. {
  288. unsigned long limit = loops_per_jiffy << 1;
  289. do {
  290. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  291. pxa2xx_spi_read(drv_data, SSDR);
  292. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  293. write_SSSR_CS(drv_data, SSSR_ROR);
  294. return limit;
  295. }
  296. static int null_writer(struct driver_data *drv_data)
  297. {
  298. u8 n_bytes = drv_data->n_bytes;
  299. if (pxa2xx_spi_txfifo_full(drv_data)
  300. || (drv_data->tx == drv_data->tx_end))
  301. return 0;
  302. pxa2xx_spi_write(drv_data, SSDR, 0);
  303. drv_data->tx += n_bytes;
  304. return 1;
  305. }
  306. static int null_reader(struct driver_data *drv_data)
  307. {
  308. u8 n_bytes = drv_data->n_bytes;
  309. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  310. && (drv_data->rx < drv_data->rx_end)) {
  311. pxa2xx_spi_read(drv_data, SSDR);
  312. drv_data->rx += n_bytes;
  313. }
  314. return drv_data->rx == drv_data->rx_end;
  315. }
  316. static int u8_writer(struct driver_data *drv_data)
  317. {
  318. if (pxa2xx_spi_txfifo_full(drv_data)
  319. || (drv_data->tx == drv_data->tx_end))
  320. return 0;
  321. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  322. ++drv_data->tx;
  323. return 1;
  324. }
  325. static int u8_reader(struct driver_data *drv_data)
  326. {
  327. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  328. && (drv_data->rx < drv_data->rx_end)) {
  329. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  330. ++drv_data->rx;
  331. }
  332. return drv_data->rx == drv_data->rx_end;
  333. }
  334. static int u16_writer(struct driver_data *drv_data)
  335. {
  336. if (pxa2xx_spi_txfifo_full(drv_data)
  337. || (drv_data->tx == drv_data->tx_end))
  338. return 0;
  339. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  340. drv_data->tx += 2;
  341. return 1;
  342. }
  343. static int u16_reader(struct driver_data *drv_data)
  344. {
  345. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  346. && (drv_data->rx < drv_data->rx_end)) {
  347. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  348. drv_data->rx += 2;
  349. }
  350. return drv_data->rx == drv_data->rx_end;
  351. }
  352. static int u32_writer(struct driver_data *drv_data)
  353. {
  354. if (pxa2xx_spi_txfifo_full(drv_data)
  355. || (drv_data->tx == drv_data->tx_end))
  356. return 0;
  357. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  358. drv_data->tx += 4;
  359. return 1;
  360. }
  361. static int u32_reader(struct driver_data *drv_data)
  362. {
  363. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  364. && (drv_data->rx < drv_data->rx_end)) {
  365. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  366. drv_data->rx += 4;
  367. }
  368. return drv_data->rx == drv_data->rx_end;
  369. }
  370. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  371. {
  372. struct spi_message *msg = drv_data->cur_msg;
  373. struct spi_transfer *trans = drv_data->cur_transfer;
  374. /* Move to next transfer */
  375. if (trans->transfer_list.next != &msg->transfers) {
  376. drv_data->cur_transfer =
  377. list_entry(trans->transfer_list.next,
  378. struct spi_transfer,
  379. transfer_list);
  380. return RUNNING_STATE;
  381. } else
  382. return DONE_STATE;
  383. }
  384. /* caller already set message->status; dma and pio irqs are blocked */
  385. static void giveback(struct driver_data *drv_data)
  386. {
  387. struct spi_transfer* last_transfer;
  388. struct spi_message *msg;
  389. msg = drv_data->cur_msg;
  390. drv_data->cur_msg = NULL;
  391. drv_data->cur_transfer = NULL;
  392. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  393. transfer_list);
  394. /* Delay if requested before any change in chip select */
  395. if (last_transfer->delay_usecs)
  396. udelay(last_transfer->delay_usecs);
  397. /* Drop chip select UNLESS cs_change is true or we are returning
  398. * a message with an error, or next message is for another chip
  399. */
  400. if (!last_transfer->cs_change)
  401. cs_deassert(drv_data);
  402. else {
  403. struct spi_message *next_msg;
  404. /* Holding of cs was hinted, but we need to make sure
  405. * the next message is for the same chip. Don't waste
  406. * time with the following tests unless this was hinted.
  407. *
  408. * We cannot postpone this until pump_messages, because
  409. * after calling msg->complete (below) the driver that
  410. * sent the current message could be unloaded, which
  411. * could invalidate the cs_control() callback...
  412. */
  413. /* get a pointer to the next message, if any */
  414. next_msg = spi_get_next_queued_message(drv_data->master);
  415. /* see if the next and current messages point
  416. * to the same chip
  417. */
  418. if (next_msg && next_msg->spi != msg->spi)
  419. next_msg = NULL;
  420. if (!next_msg || msg->state == ERROR_STATE)
  421. cs_deassert(drv_data);
  422. }
  423. drv_data->cur_chip = NULL;
  424. spi_finalize_current_message(drv_data->master);
  425. }
  426. static void reset_sccr1(struct driver_data *drv_data)
  427. {
  428. struct chip_data *chip = drv_data->cur_chip;
  429. u32 sccr1_reg;
  430. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  431. sccr1_reg &= ~SSCR1_RFT;
  432. sccr1_reg |= chip->threshold;
  433. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  434. }
  435. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  436. {
  437. /* Stop and reset SSP */
  438. write_SSSR_CS(drv_data, drv_data->clear_sr);
  439. reset_sccr1(drv_data);
  440. if (!pxa25x_ssp_comp(drv_data))
  441. pxa2xx_spi_write(drv_data, SSTO, 0);
  442. pxa2xx_spi_flush(drv_data);
  443. pxa2xx_spi_write(drv_data, SSCR0,
  444. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  445. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  446. drv_data->cur_msg->state = ERROR_STATE;
  447. tasklet_schedule(&drv_data->pump_transfers);
  448. }
  449. static void int_transfer_complete(struct driver_data *drv_data)
  450. {
  451. /* Stop SSP */
  452. write_SSSR_CS(drv_data, drv_data->clear_sr);
  453. reset_sccr1(drv_data);
  454. if (!pxa25x_ssp_comp(drv_data))
  455. pxa2xx_spi_write(drv_data, SSTO, 0);
  456. /* Update total byte transferred return count actual bytes read */
  457. drv_data->cur_msg->actual_length += drv_data->len -
  458. (drv_data->rx_end - drv_data->rx);
  459. /* Transfer delays and chip select release are
  460. * handled in pump_transfers or giveback
  461. */
  462. /* Move to next transfer */
  463. drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  464. /* Schedule transfer tasklet */
  465. tasklet_schedule(&drv_data->pump_transfers);
  466. }
  467. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  468. {
  469. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  470. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  471. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  472. if (irq_status & SSSR_ROR) {
  473. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  474. return IRQ_HANDLED;
  475. }
  476. if (irq_status & SSSR_TINT) {
  477. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  478. if (drv_data->read(drv_data)) {
  479. int_transfer_complete(drv_data);
  480. return IRQ_HANDLED;
  481. }
  482. }
  483. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  484. do {
  485. if (drv_data->read(drv_data)) {
  486. int_transfer_complete(drv_data);
  487. return IRQ_HANDLED;
  488. }
  489. } while (drv_data->write(drv_data));
  490. if (drv_data->read(drv_data)) {
  491. int_transfer_complete(drv_data);
  492. return IRQ_HANDLED;
  493. }
  494. if (drv_data->tx == drv_data->tx_end) {
  495. u32 bytes_left;
  496. u32 sccr1_reg;
  497. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  498. sccr1_reg &= ~SSCR1_TIE;
  499. /*
  500. * PXA25x_SSP has no timeout, set up rx threshould for the
  501. * remaining RX bytes.
  502. */
  503. if (pxa25x_ssp_comp(drv_data)) {
  504. u32 rx_thre;
  505. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  506. bytes_left = drv_data->rx_end - drv_data->rx;
  507. switch (drv_data->n_bytes) {
  508. case 4:
  509. bytes_left >>= 1;
  510. case 2:
  511. bytes_left >>= 1;
  512. }
  513. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  514. if (rx_thre > bytes_left)
  515. rx_thre = bytes_left;
  516. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  517. }
  518. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  519. }
  520. /* We did something */
  521. return IRQ_HANDLED;
  522. }
  523. static irqreturn_t ssp_int(int irq, void *dev_id)
  524. {
  525. struct driver_data *drv_data = dev_id;
  526. u32 sccr1_reg;
  527. u32 mask = drv_data->mask_sr;
  528. u32 status;
  529. /*
  530. * The IRQ might be shared with other peripherals so we must first
  531. * check that are we RPM suspended or not. If we are we assume that
  532. * the IRQ was not for us (we shouldn't be RPM suspended when the
  533. * interrupt is enabled).
  534. */
  535. if (pm_runtime_suspended(&drv_data->pdev->dev))
  536. return IRQ_NONE;
  537. /*
  538. * If the device is not yet in RPM suspended state and we get an
  539. * interrupt that is meant for another device, check if status bits
  540. * are all set to one. That means that the device is already
  541. * powered off.
  542. */
  543. status = pxa2xx_spi_read(drv_data, SSSR);
  544. if (status == ~0)
  545. return IRQ_NONE;
  546. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  547. /* Ignore possible writes if we don't need to write */
  548. if (!(sccr1_reg & SSCR1_TIE))
  549. mask &= ~SSSR_TFS;
  550. /* Ignore RX timeout interrupt if it is disabled */
  551. if (!(sccr1_reg & SSCR1_TINTE))
  552. mask &= ~SSSR_TINT;
  553. if (!(status & mask))
  554. return IRQ_NONE;
  555. if (!drv_data->cur_msg) {
  556. pxa2xx_spi_write(drv_data, SSCR0,
  557. pxa2xx_spi_read(drv_data, SSCR0)
  558. & ~SSCR0_SSE);
  559. pxa2xx_spi_write(drv_data, SSCR1,
  560. pxa2xx_spi_read(drv_data, SSCR1)
  561. & ~drv_data->int_cr1);
  562. if (!pxa25x_ssp_comp(drv_data))
  563. pxa2xx_spi_write(drv_data, SSTO, 0);
  564. write_SSSR_CS(drv_data, drv_data->clear_sr);
  565. dev_err(&drv_data->pdev->dev,
  566. "bad message state in interrupt handler\n");
  567. /* Never fail */
  568. return IRQ_HANDLED;
  569. }
  570. return drv_data->transfer_handler(drv_data);
  571. }
  572. /*
  573. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  574. * input frequency by fractions of 2^24. It also has a divider by 5.
  575. *
  576. * There are formulas to get baud rate value for given input frequency and
  577. * divider parameters, such as DDS_CLK_RATE and SCR:
  578. *
  579. * Fsys = 200MHz
  580. *
  581. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  582. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  583. *
  584. * DDS_CLK_RATE either 2^n or 2^n / 5.
  585. * SCR is in range 0 .. 255
  586. *
  587. * Divisor = 5^i * 2^j * 2 * k
  588. * i = [0, 1] i = 1 iff j = 0 or j > 3
  589. * j = [0, 23] j = 0 iff i = 1
  590. * k = [1, 256]
  591. * Special case: j = 0, i = 1: Divisor = 2 / 5
  592. *
  593. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  594. * are:
  595. * Case 1: 2^n, n = [0, 23]
  596. * Case 2: 2^24 * 2 / 5 (0x666666)
  597. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  598. *
  599. * In all cases the lowest possible value is better.
  600. *
  601. * The function calculates parameters for all cases and chooses the one closest
  602. * to the asked baud rate.
  603. */
  604. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  605. {
  606. unsigned long xtal = 200000000;
  607. unsigned long fref = xtal / 2; /* mandatory division by 2,
  608. see (2) */
  609. /* case 3 */
  610. unsigned long fref1 = fref / 2; /* case 1 */
  611. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  612. unsigned long scale;
  613. unsigned long q, q1, q2;
  614. long r, r1, r2;
  615. u32 mul;
  616. /* Case 1 */
  617. /* Set initial value for DDS_CLK_RATE */
  618. mul = (1 << 24) >> 1;
  619. /* Calculate initial quot */
  620. q1 = DIV_ROUND_CLOSEST(fref1, rate);
  621. /* Scale q1 if it's too big */
  622. if (q1 > 256) {
  623. /* Scale q1 to range [1, 512] */
  624. scale = fls_long(q1 - 1);
  625. if (scale > 9) {
  626. q1 >>= scale - 9;
  627. mul >>= scale - 9;
  628. }
  629. /* Round the result if we have a remainder */
  630. q1 += q1 & 1;
  631. }
  632. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  633. scale = __ffs(q1);
  634. q1 >>= scale;
  635. mul >>= scale;
  636. /* Get the remainder */
  637. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  638. /* Case 2 */
  639. q2 = DIV_ROUND_CLOSEST(fref2, rate);
  640. r2 = abs(fref2 / q2 - rate);
  641. /*
  642. * Choose the best between two: less remainder we have the better. We
  643. * can't go case 2 if q2 is greater than 256 since SCR register can
  644. * hold only values 0 .. 255.
  645. */
  646. if (r2 >= r1 || q2 > 256) {
  647. /* case 1 is better */
  648. r = r1;
  649. q = q1;
  650. } else {
  651. /* case 2 is better */
  652. r = r2;
  653. q = q2;
  654. mul = (1 << 24) * 2 / 5;
  655. }
  656. /* Check case 3 only If the divisor is big enough */
  657. if (fref / rate >= 80) {
  658. u64 fssp;
  659. u32 m;
  660. /* Calculate initial quot */
  661. q1 = DIV_ROUND_CLOSEST(fref, rate);
  662. m = (1 << 24) / q1;
  663. /* Get the remainder */
  664. fssp = (u64)fref * m;
  665. do_div(fssp, 1 << 24);
  666. r1 = abs(fssp - rate);
  667. /* Choose this one if it suits better */
  668. if (r1 < r) {
  669. /* case 3 is better */
  670. q = 1;
  671. mul = m;
  672. }
  673. }
  674. *dds = mul;
  675. return q - 1;
  676. }
  677. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  678. {
  679. unsigned long ssp_clk = drv_data->max_clk_rate;
  680. const struct ssp_device *ssp = drv_data->ssp;
  681. rate = min_t(int, ssp_clk, rate);
  682. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  683. return (ssp_clk / (2 * rate) - 1) & 0xff;
  684. else
  685. return (ssp_clk / rate - 1) & 0xfff;
  686. }
  687. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  688. struct chip_data *chip, int rate)
  689. {
  690. unsigned int clk_div;
  691. switch (drv_data->ssp_type) {
  692. case QUARK_X1000_SSP:
  693. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  694. break;
  695. default:
  696. clk_div = ssp_get_clk_div(drv_data, rate);
  697. break;
  698. }
  699. return clk_div << 8;
  700. }
  701. static void pump_transfers(unsigned long data)
  702. {
  703. struct driver_data *drv_data = (struct driver_data *)data;
  704. struct spi_message *message = NULL;
  705. struct spi_transfer *transfer = NULL;
  706. struct spi_transfer *previous = NULL;
  707. struct chip_data *chip = NULL;
  708. u32 clk_div = 0;
  709. u8 bits = 0;
  710. u32 speed = 0;
  711. u32 cr0;
  712. u32 cr1;
  713. u32 dma_thresh = drv_data->cur_chip->dma_threshold;
  714. u32 dma_burst = drv_data->cur_chip->dma_burst_size;
  715. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  716. /* Get current state information */
  717. message = drv_data->cur_msg;
  718. transfer = drv_data->cur_transfer;
  719. chip = drv_data->cur_chip;
  720. /* Handle for abort */
  721. if (message->state == ERROR_STATE) {
  722. message->status = -EIO;
  723. giveback(drv_data);
  724. return;
  725. }
  726. /* Handle end of message */
  727. if (message->state == DONE_STATE) {
  728. message->status = 0;
  729. giveback(drv_data);
  730. return;
  731. }
  732. /* Delay if requested at end of transfer before CS change */
  733. if (message->state == RUNNING_STATE) {
  734. previous = list_entry(transfer->transfer_list.prev,
  735. struct spi_transfer,
  736. transfer_list);
  737. if (previous->delay_usecs)
  738. udelay(previous->delay_usecs);
  739. /* Drop chip select only if cs_change is requested */
  740. if (previous->cs_change)
  741. cs_deassert(drv_data);
  742. }
  743. /* Check if we can DMA this transfer */
  744. if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
  745. /* reject already-mapped transfers; PIO won't always work */
  746. if (message->is_dma_mapped
  747. || transfer->rx_dma || transfer->tx_dma) {
  748. dev_err(&drv_data->pdev->dev,
  749. "pump_transfers: mapped transfer length of "
  750. "%u is greater than %d\n",
  751. transfer->len, MAX_DMA_LEN);
  752. message->status = -EINVAL;
  753. giveback(drv_data);
  754. return;
  755. }
  756. /* warn ... we force this to PIO mode */
  757. dev_warn_ratelimited(&message->spi->dev,
  758. "pump_transfers: DMA disabled for transfer length %ld "
  759. "greater than %d\n",
  760. (long)drv_data->len, MAX_DMA_LEN);
  761. }
  762. /* Setup the transfer state based on the type of transfer */
  763. if (pxa2xx_spi_flush(drv_data) == 0) {
  764. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  765. message->status = -EIO;
  766. giveback(drv_data);
  767. return;
  768. }
  769. drv_data->n_bytes = chip->n_bytes;
  770. drv_data->tx = (void *)transfer->tx_buf;
  771. drv_data->tx_end = drv_data->tx + transfer->len;
  772. drv_data->rx = transfer->rx_buf;
  773. drv_data->rx_end = drv_data->rx + transfer->len;
  774. drv_data->rx_dma = transfer->rx_dma;
  775. drv_data->tx_dma = transfer->tx_dma;
  776. drv_data->len = transfer->len;
  777. drv_data->write = drv_data->tx ? chip->write : null_writer;
  778. drv_data->read = drv_data->rx ? chip->read : null_reader;
  779. /* Change speed and bit per word on a per transfer */
  780. cr0 = chip->cr0;
  781. if (transfer->speed_hz || transfer->bits_per_word) {
  782. bits = chip->bits_per_word;
  783. speed = chip->speed_hz;
  784. if (transfer->speed_hz)
  785. speed = transfer->speed_hz;
  786. if (transfer->bits_per_word)
  787. bits = transfer->bits_per_word;
  788. clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
  789. if (bits <= 8) {
  790. drv_data->n_bytes = 1;
  791. drv_data->read = drv_data->read != null_reader ?
  792. u8_reader : null_reader;
  793. drv_data->write = drv_data->write != null_writer ?
  794. u8_writer : null_writer;
  795. } else if (bits <= 16) {
  796. drv_data->n_bytes = 2;
  797. drv_data->read = drv_data->read != null_reader ?
  798. u16_reader : null_reader;
  799. drv_data->write = drv_data->write != null_writer ?
  800. u16_writer : null_writer;
  801. } else if (bits <= 32) {
  802. drv_data->n_bytes = 4;
  803. drv_data->read = drv_data->read != null_reader ?
  804. u32_reader : null_reader;
  805. drv_data->write = drv_data->write != null_writer ?
  806. u32_writer : null_writer;
  807. }
  808. /* if bits/word is changed in dma mode, then must check the
  809. * thresholds and burst also */
  810. if (chip->enable_dma) {
  811. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  812. message->spi,
  813. bits, &dma_burst,
  814. &dma_thresh))
  815. dev_warn_ratelimited(&message->spi->dev,
  816. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  817. }
  818. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  819. }
  820. message->state = RUNNING_STATE;
  821. drv_data->dma_mapped = 0;
  822. if (pxa2xx_spi_dma_is_possible(drv_data->len))
  823. drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
  824. if (drv_data->dma_mapped) {
  825. /* Ensure we have the correct interrupt handler */
  826. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  827. pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  828. /* Clear status and start DMA engine */
  829. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  830. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  831. pxa2xx_spi_dma_start(drv_data);
  832. } else {
  833. /* Ensure we have the correct interrupt handler */
  834. drv_data->transfer_handler = interrupt_transfer;
  835. /* Clear status */
  836. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  837. write_SSSR_CS(drv_data, drv_data->clear_sr);
  838. }
  839. if (is_lpss_ssp(drv_data)) {
  840. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  841. != chip->lpss_rx_threshold)
  842. pxa2xx_spi_write(drv_data, SSIRF,
  843. chip->lpss_rx_threshold);
  844. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  845. != chip->lpss_tx_threshold)
  846. pxa2xx_spi_write(drv_data, SSITF,
  847. chip->lpss_tx_threshold);
  848. }
  849. if (is_quark_x1000_ssp(drv_data) &&
  850. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  851. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  852. /* see if we need to reload the config registers */
  853. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  854. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  855. != (cr1 & change_mask)) {
  856. /* stop the SSP, and update the other bits */
  857. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  858. if (!pxa25x_ssp_comp(drv_data))
  859. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  860. /* first set CR1 without interrupt and service enables */
  861. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  862. /* restart the SSP */
  863. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  864. } else {
  865. if (!pxa25x_ssp_comp(drv_data))
  866. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  867. }
  868. cs_assert(drv_data);
  869. /* after chip select, release the data by enabling service
  870. * requests and interrupts, without changing any mode bits */
  871. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  872. }
  873. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  874. struct spi_message *msg)
  875. {
  876. struct driver_data *drv_data = spi_master_get_devdata(master);
  877. drv_data->cur_msg = msg;
  878. /* Initial message state*/
  879. drv_data->cur_msg->state = START_STATE;
  880. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  881. struct spi_transfer,
  882. transfer_list);
  883. /* prepare to setup the SSP, in pump_transfers, using the per
  884. * chip configuration */
  885. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  886. /* Mark as busy and launch transfers */
  887. tasklet_schedule(&drv_data->pump_transfers);
  888. return 0;
  889. }
  890. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  891. {
  892. struct driver_data *drv_data = spi_master_get_devdata(master);
  893. /* Disable the SSP now */
  894. pxa2xx_spi_write(drv_data, SSCR0,
  895. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  896. return 0;
  897. }
  898. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  899. struct pxa2xx_spi_chip *chip_info)
  900. {
  901. int err = 0;
  902. if (chip == NULL || chip_info == NULL)
  903. return 0;
  904. /* NOTE: setup() can be called multiple times, possibly with
  905. * different chip_info, release previously requested GPIO
  906. */
  907. if (gpio_is_valid(chip->gpio_cs))
  908. gpio_free(chip->gpio_cs);
  909. /* If (*cs_control) is provided, ignore GPIO chip select */
  910. if (chip_info->cs_control) {
  911. chip->cs_control = chip_info->cs_control;
  912. return 0;
  913. }
  914. if (gpio_is_valid(chip_info->gpio_cs)) {
  915. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  916. if (err) {
  917. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  918. chip_info->gpio_cs);
  919. return err;
  920. }
  921. chip->gpio_cs = chip_info->gpio_cs;
  922. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  923. err = gpio_direction_output(chip->gpio_cs,
  924. !chip->gpio_cs_inverted);
  925. }
  926. return err;
  927. }
  928. static int setup(struct spi_device *spi)
  929. {
  930. struct pxa2xx_spi_chip *chip_info = NULL;
  931. struct chip_data *chip;
  932. const struct lpss_config *config;
  933. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  934. unsigned int clk_div;
  935. uint tx_thres, tx_hi_thres, rx_thres;
  936. switch (drv_data->ssp_type) {
  937. case QUARK_X1000_SSP:
  938. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  939. tx_hi_thres = 0;
  940. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  941. break;
  942. case LPSS_LPT_SSP:
  943. case LPSS_BYT_SSP:
  944. case LPSS_SPT_SSP:
  945. config = lpss_get_config(drv_data);
  946. tx_thres = config->tx_threshold_lo;
  947. tx_hi_thres = config->tx_threshold_hi;
  948. rx_thres = config->rx_threshold;
  949. break;
  950. default:
  951. tx_thres = TX_THRESH_DFLT;
  952. tx_hi_thres = 0;
  953. rx_thres = RX_THRESH_DFLT;
  954. break;
  955. }
  956. /* Only alloc on first setup */
  957. chip = spi_get_ctldata(spi);
  958. if (!chip) {
  959. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  960. if (!chip)
  961. return -ENOMEM;
  962. if (drv_data->ssp_type == CE4100_SSP) {
  963. if (spi->chip_select > 4) {
  964. dev_err(&spi->dev,
  965. "failed setup: cs number must not be > 4.\n");
  966. kfree(chip);
  967. return -EINVAL;
  968. }
  969. chip->frm = spi->chip_select;
  970. } else
  971. chip->gpio_cs = -1;
  972. chip->enable_dma = 0;
  973. chip->timeout = TIMOUT_DFLT;
  974. }
  975. /* protocol drivers may change the chip settings, so...
  976. * if chip_info exists, use it */
  977. chip_info = spi->controller_data;
  978. /* chip_info isn't always needed */
  979. chip->cr1 = 0;
  980. if (chip_info) {
  981. if (chip_info->timeout)
  982. chip->timeout = chip_info->timeout;
  983. if (chip_info->tx_threshold)
  984. tx_thres = chip_info->tx_threshold;
  985. if (chip_info->tx_hi_threshold)
  986. tx_hi_thres = chip_info->tx_hi_threshold;
  987. if (chip_info->rx_threshold)
  988. rx_thres = chip_info->rx_threshold;
  989. chip->enable_dma = drv_data->master_info->enable_dma;
  990. chip->dma_threshold = 0;
  991. if (chip_info->enable_loopback)
  992. chip->cr1 = SSCR1_LBM;
  993. } else if (ACPI_HANDLE(&spi->dev)) {
  994. /*
  995. * Slave devices enumerated from ACPI namespace don't
  996. * usually have chip_info but we still might want to use
  997. * DMA with them.
  998. */
  999. chip->enable_dma = drv_data->master_info->enable_dma;
  1000. }
  1001. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1002. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1003. | SSITF_TxHiThresh(tx_hi_thres);
  1004. /* set dma burst and threshold outside of chip_info path so that if
  1005. * chip_info goes away after setting chip->enable_dma, the
  1006. * burst and threshold can still respond to changes in bits_per_word */
  1007. if (chip->enable_dma) {
  1008. /* set up legal burst and threshold for dma */
  1009. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1010. spi->bits_per_word,
  1011. &chip->dma_burst_size,
  1012. &chip->dma_threshold)) {
  1013. dev_warn(&spi->dev,
  1014. "in setup: DMA burst size reduced to match bits_per_word\n");
  1015. }
  1016. }
  1017. clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
  1018. chip->speed_hz = spi->max_speed_hz;
  1019. chip->cr0 = pxa2xx_configure_sscr0(drv_data, clk_div,
  1020. spi->bits_per_word);
  1021. switch (drv_data->ssp_type) {
  1022. case QUARK_X1000_SSP:
  1023. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1024. & QUARK_X1000_SSCR1_RFT)
  1025. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1026. & QUARK_X1000_SSCR1_TFT);
  1027. break;
  1028. default:
  1029. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1030. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1031. break;
  1032. }
  1033. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1034. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1035. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1036. if (spi->mode & SPI_LOOP)
  1037. chip->cr1 |= SSCR1_LBM;
  1038. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  1039. if (!pxa25x_ssp_comp(drv_data))
  1040. dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
  1041. drv_data->max_clk_rate
  1042. / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
  1043. chip->enable_dma ? "DMA" : "PIO");
  1044. else
  1045. dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
  1046. drv_data->max_clk_rate / 2
  1047. / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  1048. chip->enable_dma ? "DMA" : "PIO");
  1049. if (spi->bits_per_word <= 8) {
  1050. chip->n_bytes = 1;
  1051. chip->read = u8_reader;
  1052. chip->write = u8_writer;
  1053. } else if (spi->bits_per_word <= 16) {
  1054. chip->n_bytes = 2;
  1055. chip->read = u16_reader;
  1056. chip->write = u16_writer;
  1057. } else if (spi->bits_per_word <= 32) {
  1058. if (!is_quark_x1000_ssp(drv_data))
  1059. chip->cr0 |= SSCR0_EDSS;
  1060. chip->n_bytes = 4;
  1061. chip->read = u32_reader;
  1062. chip->write = u32_writer;
  1063. }
  1064. chip->bits_per_word = spi->bits_per_word;
  1065. spi_set_ctldata(spi, chip);
  1066. if (drv_data->ssp_type == CE4100_SSP)
  1067. return 0;
  1068. return setup_cs(spi, chip, chip_info);
  1069. }
  1070. static void cleanup(struct spi_device *spi)
  1071. {
  1072. struct chip_data *chip = spi_get_ctldata(spi);
  1073. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1074. if (!chip)
  1075. return;
  1076. if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
  1077. gpio_free(chip->gpio_cs);
  1078. kfree(chip);
  1079. }
  1080. #ifdef CONFIG_ACPI
  1081. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1082. { "INT33C0", LPSS_LPT_SSP },
  1083. { "INT33C1", LPSS_LPT_SSP },
  1084. { "INT3430", LPSS_LPT_SSP },
  1085. { "INT3431", LPSS_LPT_SSP },
  1086. { "80860F0E", LPSS_BYT_SSP },
  1087. { "8086228E", LPSS_BYT_SSP },
  1088. { },
  1089. };
  1090. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1091. /*
  1092. * PCI IDs of compound devices that integrate both host controller and private
  1093. * integrated DMA engine. Please note these are not used in module
  1094. * autoloading and probing in this module but matching the LPSS SSP type.
  1095. */
  1096. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1097. /* SPT-LP */
  1098. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1099. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1100. /* SPT-H */
  1101. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1102. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1103. { },
  1104. };
  1105. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1106. {
  1107. struct device *dev = param;
  1108. if (dev != chan->device->dev->parent)
  1109. return false;
  1110. return true;
  1111. }
  1112. static struct pxa2xx_spi_master *
  1113. pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
  1114. {
  1115. struct pxa2xx_spi_master *pdata;
  1116. struct acpi_device *adev;
  1117. struct ssp_device *ssp;
  1118. struct resource *res;
  1119. const struct acpi_device_id *adev_id = NULL;
  1120. const struct pci_device_id *pcidev_id = NULL;
  1121. int devid, type;
  1122. if (!ACPI_HANDLE(&pdev->dev) ||
  1123. acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
  1124. return NULL;
  1125. if (dev_is_pci(pdev->dev.parent))
  1126. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1127. to_pci_dev(pdev->dev.parent));
  1128. else
  1129. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1130. &pdev->dev);
  1131. if (adev_id)
  1132. type = (int)adev_id->driver_data;
  1133. else if (pcidev_id)
  1134. type = (int)pcidev_id->driver_data;
  1135. else
  1136. return NULL;
  1137. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1138. if (!pdata)
  1139. return NULL;
  1140. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1141. if (!res)
  1142. return NULL;
  1143. ssp = &pdata->ssp;
  1144. ssp->phys_base = res->start;
  1145. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1146. if (IS_ERR(ssp->mmio_base))
  1147. return NULL;
  1148. if (pcidev_id) {
  1149. pdata->tx_param = pdev->dev.parent;
  1150. pdata->rx_param = pdev->dev.parent;
  1151. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1152. }
  1153. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1154. ssp->irq = platform_get_irq(pdev, 0);
  1155. ssp->type = type;
  1156. ssp->pdev = pdev;
  1157. ssp->port_id = -1;
  1158. if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
  1159. ssp->port_id = devid;
  1160. pdata->num_chipselect = 1;
  1161. pdata->enable_dma = true;
  1162. return pdata;
  1163. }
  1164. #else
  1165. static inline struct pxa2xx_spi_master *
  1166. pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
  1167. {
  1168. return NULL;
  1169. }
  1170. #endif
  1171. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1172. {
  1173. struct device *dev = &pdev->dev;
  1174. struct pxa2xx_spi_master *platform_info;
  1175. struct spi_master *master;
  1176. struct driver_data *drv_data;
  1177. struct ssp_device *ssp;
  1178. int status;
  1179. u32 tmp;
  1180. platform_info = dev_get_platdata(dev);
  1181. if (!platform_info) {
  1182. platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
  1183. if (!platform_info) {
  1184. dev_err(&pdev->dev, "missing platform data\n");
  1185. return -ENODEV;
  1186. }
  1187. }
  1188. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1189. if (!ssp)
  1190. ssp = &platform_info->ssp;
  1191. if (!ssp->mmio_base) {
  1192. dev_err(&pdev->dev, "failed to get ssp\n");
  1193. return -ENODEV;
  1194. }
  1195. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1196. if (!master) {
  1197. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1198. pxa_ssp_free(ssp);
  1199. return -ENOMEM;
  1200. }
  1201. drv_data = spi_master_get_devdata(master);
  1202. drv_data->master = master;
  1203. drv_data->master_info = platform_info;
  1204. drv_data->pdev = pdev;
  1205. drv_data->ssp = ssp;
  1206. master->dev.parent = &pdev->dev;
  1207. master->dev.of_node = pdev->dev.of_node;
  1208. /* the spi->mode bits understood by this driver: */
  1209. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1210. master->bus_num = ssp->port_id;
  1211. master->num_chipselect = platform_info->num_chipselect;
  1212. master->dma_alignment = DMA_ALIGNMENT;
  1213. master->cleanup = cleanup;
  1214. master->setup = setup;
  1215. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1216. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1217. master->auto_runtime_pm = true;
  1218. drv_data->ssp_type = ssp->type;
  1219. drv_data->ioaddr = ssp->mmio_base;
  1220. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1221. if (pxa25x_ssp_comp(drv_data)) {
  1222. switch (drv_data->ssp_type) {
  1223. case QUARK_X1000_SSP:
  1224. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1225. break;
  1226. default:
  1227. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1228. break;
  1229. }
  1230. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1231. drv_data->dma_cr1 = 0;
  1232. drv_data->clear_sr = SSSR_ROR;
  1233. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1234. } else {
  1235. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1236. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1237. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1238. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1239. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1240. }
  1241. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1242. drv_data);
  1243. if (status < 0) {
  1244. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1245. goto out_error_master_alloc;
  1246. }
  1247. /* Setup DMA if requested */
  1248. if (platform_info->enable_dma) {
  1249. status = pxa2xx_spi_dma_setup(drv_data);
  1250. if (status) {
  1251. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1252. platform_info->enable_dma = false;
  1253. }
  1254. }
  1255. /* Enable SOC clock */
  1256. clk_prepare_enable(ssp->clk);
  1257. drv_data->max_clk_rate = clk_get_rate(ssp->clk);
  1258. /* Load default SSP configuration */
  1259. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1260. switch (drv_data->ssp_type) {
  1261. case QUARK_X1000_SSP:
  1262. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
  1263. | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1264. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1265. /* using the Motorola SPI protocol and use 8 bit frame */
  1266. pxa2xx_spi_write(drv_data, SSCR0,
  1267. QUARK_X1000_SSCR0_Motorola
  1268. | QUARK_X1000_SSCR0_DataSize(8));
  1269. break;
  1270. default:
  1271. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1272. SSCR1_TxTresh(TX_THRESH_DFLT);
  1273. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1274. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1275. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1276. break;
  1277. }
  1278. if (!pxa25x_ssp_comp(drv_data))
  1279. pxa2xx_spi_write(drv_data, SSTO, 0);
  1280. if (!is_quark_x1000_ssp(drv_data))
  1281. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1282. if (is_lpss_ssp(drv_data))
  1283. lpss_ssp_setup(drv_data);
  1284. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1285. (unsigned long)drv_data);
  1286. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1287. pm_runtime_use_autosuspend(&pdev->dev);
  1288. pm_runtime_set_active(&pdev->dev);
  1289. pm_runtime_enable(&pdev->dev);
  1290. /* Register with the SPI framework */
  1291. platform_set_drvdata(pdev, drv_data);
  1292. status = devm_spi_register_master(&pdev->dev, master);
  1293. if (status != 0) {
  1294. dev_err(&pdev->dev, "problem registering spi master\n");
  1295. goto out_error_clock_enabled;
  1296. }
  1297. return status;
  1298. out_error_clock_enabled:
  1299. clk_disable_unprepare(ssp->clk);
  1300. pxa2xx_spi_dma_release(drv_data);
  1301. free_irq(ssp->irq, drv_data);
  1302. out_error_master_alloc:
  1303. spi_master_put(master);
  1304. pxa_ssp_free(ssp);
  1305. return status;
  1306. }
  1307. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1308. {
  1309. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1310. struct ssp_device *ssp;
  1311. if (!drv_data)
  1312. return 0;
  1313. ssp = drv_data->ssp;
  1314. pm_runtime_get_sync(&pdev->dev);
  1315. /* Disable the SSP at the peripheral and SOC level */
  1316. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1317. clk_disable_unprepare(ssp->clk);
  1318. /* Release DMA */
  1319. if (drv_data->master_info->enable_dma)
  1320. pxa2xx_spi_dma_release(drv_data);
  1321. pm_runtime_put_noidle(&pdev->dev);
  1322. pm_runtime_disable(&pdev->dev);
  1323. /* Release IRQ */
  1324. free_irq(ssp->irq, drv_data);
  1325. /* Release SSP */
  1326. pxa_ssp_free(ssp);
  1327. return 0;
  1328. }
  1329. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1330. {
  1331. int status = 0;
  1332. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1333. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1334. }
  1335. #ifdef CONFIG_PM_SLEEP
  1336. static int pxa2xx_spi_suspend(struct device *dev)
  1337. {
  1338. struct driver_data *drv_data = dev_get_drvdata(dev);
  1339. struct ssp_device *ssp = drv_data->ssp;
  1340. int status = 0;
  1341. status = spi_master_suspend(drv_data->master);
  1342. if (status != 0)
  1343. return status;
  1344. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1345. if (!pm_runtime_suspended(dev))
  1346. clk_disable_unprepare(ssp->clk);
  1347. return 0;
  1348. }
  1349. static int pxa2xx_spi_resume(struct device *dev)
  1350. {
  1351. struct driver_data *drv_data = dev_get_drvdata(dev);
  1352. struct ssp_device *ssp = drv_data->ssp;
  1353. int status = 0;
  1354. pxa2xx_spi_dma_resume(drv_data);
  1355. /* Enable the SSP clock */
  1356. if (!pm_runtime_suspended(dev))
  1357. clk_prepare_enable(ssp->clk);
  1358. /* Restore LPSS private register bits */
  1359. if (is_lpss_ssp(drv_data))
  1360. lpss_ssp_setup(drv_data);
  1361. /* Start the queue running */
  1362. status = spi_master_resume(drv_data->master);
  1363. if (status != 0) {
  1364. dev_err(dev, "problem starting queue (%d)\n", status);
  1365. return status;
  1366. }
  1367. return 0;
  1368. }
  1369. #endif
  1370. #ifdef CONFIG_PM
  1371. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1372. {
  1373. struct driver_data *drv_data = dev_get_drvdata(dev);
  1374. clk_disable_unprepare(drv_data->ssp->clk);
  1375. return 0;
  1376. }
  1377. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1378. {
  1379. struct driver_data *drv_data = dev_get_drvdata(dev);
  1380. clk_prepare_enable(drv_data->ssp->clk);
  1381. return 0;
  1382. }
  1383. #endif
  1384. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1385. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1386. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1387. pxa2xx_spi_runtime_resume, NULL)
  1388. };
  1389. static struct platform_driver driver = {
  1390. .driver = {
  1391. .name = "pxa2xx-spi",
  1392. .pm = &pxa2xx_spi_pm_ops,
  1393. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1394. },
  1395. .probe = pxa2xx_spi_probe,
  1396. .remove = pxa2xx_spi_remove,
  1397. .shutdown = pxa2xx_spi_shutdown,
  1398. };
  1399. static int __init pxa2xx_spi_init(void)
  1400. {
  1401. return platform_driver_register(&driver);
  1402. }
  1403. subsys_initcall(pxa2xx_spi_init);
  1404. static void __exit pxa2xx_spi_exit(void)
  1405. {
  1406. platform_driver_unregister(&driver);
  1407. }
  1408. module_exit(pxa2xx_spi_exit);