1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <common.h>
20#include <dm.h>
21#include <mpc8xx.h>
22#include <spi.h>
23#include <linux/delay.h>
24
25#include <asm/cpm_8xx.h>
26#include <asm/io.h>
27
28#define CPM_SPI_BASE_RX CPM_SPI_BASE
29#define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
30
31#define MAX_BUFFER 0x104
32
33static int mpc8xx_spi_probe(struct udevice *dev)
34{
35 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
36 cpm8xx_t __iomem *cp = &immr->im_cpm;
37 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
38 cbd_t __iomem *tbdf, *rbdf;
39
40
41 out_be16(&spi->spi_rpbase, 0);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 clrsetbits_be32(&cp->cp_pbpar, 0x00000001, 0x0000000E);
57
58
59
60
61
62
63
64
65 setbits_be32(&cp->cp_pbdir, 0x0000000F);
66
67
68
69
70
71
72
73
74
75 clrsetbits_be16(&cp->cp_pbodr, 0x00000007, 0x00000008);
76
77
78
79
80 out_be32(&spi->spi_rstate, 0);
81 out_be32(&spi->spi_rdp, 0);
82 out_be16(&spi->spi_rbptr, 0);
83 out_be16(&spi->spi_rbc, 0);
84 out_be32(&spi->spi_rxtmp, 0);
85 out_be32(&spi->spi_tstate, 0);
86 out_be32(&spi->spi_tdp, 0);
87 out_be16(&spi->spi_tbptr, 0);
88 out_be16(&spi->spi_tbc, 0);
89 out_be32(&spi->spi_txtmp, 0);
90
91
92
93 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
94 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
95
96
97
98
99
100
101
102
103
104
105 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
106 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
107
108
109
110 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
111 ;
112
113 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
114 CPM_CR_FLG);
115 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
116 ;
117
118
119
120 out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
121
122
123
124 out_8(&spi->spi_tfcr, SMC_EB);
125 out_8(&spi->spi_rfcr, SMC_EB);
126
127
128
129 out_be16(&spi->spi_mrblr, MAX_BUFFER);
130
131
132
133 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
134 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
135
136 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
137 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
138
139
140 out_8(&cp->cp_spim, 0);
141 out_8(&cp->cp_spie, SPI_EMASK);
142
143 return 0;
144}
145
146static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
147 const void *dout, void *din, unsigned long flags)
148{
149 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
150 cpm8xx_t __iomem *cp = &immr->im_cpm;
151 cbd_t __iomem *tbdf, *rbdf;
152 int tm;
153 size_t count = (bitlen + 7) / 8;
154
155 if (count > MAX_BUFFER)
156 return -EINVAL;
157
158 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
159 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
160
161
162 clrbits_be32(&cp->cp_pbdat, 0x0001);
163
164
165 out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
166 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
167 out_be16(&tbdf->cbd_datlen, count);
168
169
170 out_be32(&rbdf->cbd_bufaddr, (ulong)din);
171 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
172 out_be16(&rbdf->cbd_datlen, 0);
173
174 clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
175 SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
176 out_8(&cp->cp_spim, 0);
177 out_8(&cp->cp_spie, SPI_EMASK);
178
179
180 setbits_8(&cp->cp_spcom, SPI_STR);
181
182
183
184
185
186 for (tm = 0; tm < 1000; ++tm) {
187 if (in_8(&cp->cp_spie) & SPI_TXB)
188 break;
189 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
190 break;
191 udelay(1000);
192 }
193 if (tm >= 1000)
194 printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
195
196
197 setbits_be32(&cp->cp_pbdat, 0x0001);
198
199 return count;
200}
201
202static const struct dm_spi_ops mpc8xx_spi_ops = {
203 .xfer = mpc8xx_spi_xfer,
204};
205
206static const struct udevice_id mpc8xx_spi_ids[] = {
207 { .compatible = "fsl,mpc8xx-spi" },
208 { }
209};
210
211U_BOOT_DRIVER(mpc8xx_spi) = {
212 .name = "mpc8xx_spi",
213 .id = UCLASS_SPI,
214 .of_match = mpc8xx_spi_ids,
215 .ops = &mpc8xx_spi_ops,
216 .probe = mpc8xx_spi_probe,
217};
218