1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/delay.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/device.h>
23#include <linux/pm.h>
24#include <linux/pci.h>
25#include <linux/interrupt.h>
26#include <linux/sfi.h>
27#include <asm/intel-mid.h>
28#include <asm/intel_scu_ipc.h>
29
30
31#define IPCMSG_WATCHDOG_TIMER 0xF8
32#define IPCMSG_BATTERY 0xEF
33#define IPCMSG_FW_UPDATE 0xFE
34#define IPCMSG_PCNTRL 0xFF
35#define IPCMSG_FW_REVISION 0xF4
36
37
38#define IPC_CMD_PCNTRL_W 0
39#define IPC_CMD_PCNTRL_R 1
40#define IPC_CMD_PCNTRL_M 2
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#define IPC_WWBUF_SIZE 20
60#define IPC_RWBUF_SIZE 20
61#define IPC_IOC 0x100
62
63#define PCI_DEVICE_ID_LINCROFT 0x082a
64#define PCI_DEVICE_ID_PENWELL 0x080e
65#define PCI_DEVICE_ID_CLOVERVIEW 0x08ea
66#define PCI_DEVICE_ID_TANGIER 0x11a0
67
68
69struct intel_scu_ipc_pdata_t {
70 u32 i2c_base;
71 u32 i2c_len;
72 u8 irq_mode;
73};
74
75static struct intel_scu_ipc_pdata_t intel_scu_ipc_lincroft_pdata = {
76 .i2c_base = 0xff12b000,
77 .i2c_len = 0x10,
78 .irq_mode = 0,
79};
80
81
82static struct intel_scu_ipc_pdata_t intel_scu_ipc_penwell_pdata = {
83 .i2c_base = 0xff12b000,
84 .i2c_len = 0x10,
85 .irq_mode = 1,
86};
87
88static struct intel_scu_ipc_pdata_t intel_scu_ipc_tangier_pdata = {
89 .i2c_base = 0xff00d000,
90 .i2c_len = 0x10,
91 .irq_mode = 0,
92};
93
94struct intel_scu_ipc_dev {
95 struct device *dev;
96 void __iomem *ipc_base;
97 void __iomem *i2c_base;
98 struct completion cmd_complete;
99 u8 irq_mode;
100};
101
102static struct intel_scu_ipc_dev ipcdev;
103
104
105
106
107
108
109#define IPC_READ_BUFFER 0x90
110
111#define IPC_I2C_CNTRL_ADDR 0
112#define I2C_DATA_ADDR 0x04
113
114static DEFINE_MUTEX(ipclock);
115
116
117
118
119
120
121
122
123static inline void ipc_command(struct intel_scu_ipc_dev *scu, u32 cmd)
124{
125 if (scu->irq_mode) {
126 reinit_completion(&scu->cmd_complete);
127 writel(cmd | IPC_IOC, scu->ipc_base);
128 }
129 writel(cmd, scu->ipc_base);
130}
131
132
133
134
135
136
137
138static inline void ipc_data_writel(struct intel_scu_ipc_dev *scu, u32 data, u32 offset)
139{
140 writel(data, scu->ipc_base + 0x80 + offset);
141}
142
143
144
145
146
147
148
149
150static inline u8 ipc_read_status(struct intel_scu_ipc_dev *scu)
151{
152 return __raw_readl(scu->ipc_base + 0x04);
153}
154
155
156static inline u8 ipc_data_readb(struct intel_scu_ipc_dev *scu, u32 offset)
157{
158 return readb(scu->ipc_base + IPC_READ_BUFFER + offset);
159}
160
161
162static inline u32 ipc_data_readl(struct intel_scu_ipc_dev *scu, u32 offset)
163{
164 return readl(scu->ipc_base + IPC_READ_BUFFER + offset);
165}
166
167
168static inline int busy_loop(struct intel_scu_ipc_dev *scu)
169{
170 u32 status = ipc_read_status(scu);
171 u32 loop_count = 100000;
172
173
174 while ((status & BIT(0)) && --loop_count) {
175 udelay(1);
176 status = ipc_read_status(scu);
177 }
178
179 if (status & BIT(0)) {
180 dev_err(scu->dev, "IPC timed out");
181 return -ETIMEDOUT;
182 }
183
184 if (status & BIT(1))
185 return -EIO;
186
187 return 0;
188}
189
190
191static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev *scu)
192{
193 int status;
194
195 if (!wait_for_completion_timeout(&scu->cmd_complete, 3 * HZ)) {
196 dev_err(scu->dev, "IPC timed out\n");
197 return -ETIMEDOUT;
198 }
199
200 status = ipc_read_status(scu);
201 if (status & BIT(1))
202 return -EIO;
203
204 return 0;
205}
206
207static int intel_scu_ipc_check_status(struct intel_scu_ipc_dev *scu)
208{
209 return scu->irq_mode ? ipc_wait_for_interrupt(scu) : busy_loop(scu);
210}
211
212
213static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
214{
215 struct intel_scu_ipc_dev *scu = &ipcdev;
216 int nc;
217 u32 offset = 0;
218 int err;
219 u8 cbuf[IPC_WWBUF_SIZE];
220 u32 *wbuf = (u32 *)&cbuf;
221
222 memset(cbuf, 0, sizeof(cbuf));
223
224 mutex_lock(&ipclock);
225
226 if (scu->dev == NULL) {
227 mutex_unlock(&ipclock);
228 return -ENODEV;
229 }
230
231 for (nc = 0; nc < count; nc++, offset += 2) {
232 cbuf[offset] = addr[nc];
233 cbuf[offset + 1] = addr[nc] >> 8;
234 }
235
236 if (id == IPC_CMD_PCNTRL_R) {
237 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
238 ipc_data_writel(scu, wbuf[nc], offset);
239 ipc_command(scu, (count * 2) << 16 | id << 12 | 0 << 8 | op);
240 } else if (id == IPC_CMD_PCNTRL_W) {
241 for (nc = 0; nc < count; nc++, offset += 1)
242 cbuf[offset] = data[nc];
243 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
244 ipc_data_writel(scu, wbuf[nc], offset);
245 ipc_command(scu, (count * 3) << 16 | id << 12 | 0 << 8 | op);
246 } else if (id == IPC_CMD_PCNTRL_M) {
247 cbuf[offset] = data[0];
248 cbuf[offset + 1] = data[1];
249 ipc_data_writel(scu, wbuf[0], 0);
250 ipc_command(scu, 4 << 16 | id << 12 | 0 << 8 | op);
251 }
252
253 err = intel_scu_ipc_check_status(scu);
254 if (!err && id == IPC_CMD_PCNTRL_R) {
255
256 memcpy_fromio(cbuf, scu->ipc_base + 0x90, 16);
257 for (nc = 0; nc < count; nc++)
258 data[nc] = ipc_data_readb(scu, nc);
259 }
260 mutex_unlock(&ipclock);
261 return err;
262}
263
264
265
266
267
268
269
270
271
272
273
274int intel_scu_ipc_ioread8(u16 addr, u8 *data)
275{
276 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
277}
278EXPORT_SYMBOL(intel_scu_ipc_ioread8);
279
280
281
282
283
284
285
286
287
288
289
290int intel_scu_ipc_ioread16(u16 addr, u16 *data)
291{
292 u16 x[2] = {addr, addr + 1};
293 return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
294}
295EXPORT_SYMBOL(intel_scu_ipc_ioread16);
296
297
298
299
300
301
302
303
304
305
306
307int intel_scu_ipc_ioread32(u16 addr, u32 *data)
308{
309 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
310 return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
311}
312EXPORT_SYMBOL(intel_scu_ipc_ioread32);
313
314
315
316
317
318
319
320
321
322
323
324int intel_scu_ipc_iowrite8(u16 addr, u8 data)
325{
326 return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
327}
328EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
329
330
331
332
333
334
335
336
337
338
339
340int intel_scu_ipc_iowrite16(u16 addr, u16 data)
341{
342 u16 x[2] = {addr, addr + 1};
343 return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
344}
345EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
346
347
348
349
350
351
352
353
354
355
356
357int intel_scu_ipc_iowrite32(u16 addr, u32 data)
358{
359 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
360 return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
361}
362EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
378{
379 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
380}
381EXPORT_SYMBOL(intel_scu_ipc_readv);
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
398{
399 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
400}
401EXPORT_SYMBOL(intel_scu_ipc_writev);
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
419{
420 u8 data[2] = { bits, mask };
421 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
422}
423EXPORT_SYMBOL(intel_scu_ipc_update_register);
424
425
426
427
428
429
430
431
432
433
434
435
436
437int intel_scu_ipc_simple_command(int cmd, int sub)
438{
439 struct intel_scu_ipc_dev *scu = &ipcdev;
440 int err;
441
442 mutex_lock(&ipclock);
443 if (scu->dev == NULL) {
444 mutex_unlock(&ipclock);
445 return -ENODEV;
446 }
447 ipc_command(scu, sub << 12 | cmd);
448 err = intel_scu_ipc_check_status(scu);
449 mutex_unlock(&ipclock);
450 return err;
451}
452EXPORT_SYMBOL(intel_scu_ipc_simple_command);
453
454
455
456
457
458
459
460
461
462
463
464
465
466int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
467 u32 *out, int outlen)
468{
469 struct intel_scu_ipc_dev *scu = &ipcdev;
470 int i, err;
471
472 mutex_lock(&ipclock);
473 if (scu->dev == NULL) {
474 mutex_unlock(&ipclock);
475 return -ENODEV;
476 }
477
478 for (i = 0; i < inlen; i++)
479 ipc_data_writel(scu, *in++, 4 * i);
480
481 ipc_command(scu, (inlen << 16) | (sub << 12) | cmd);
482 err = intel_scu_ipc_check_status(scu);
483
484 if (!err) {
485 for (i = 0; i < outlen; i++)
486 *out++ = ipc_data_readl(scu, 4 * i);
487 }
488
489 mutex_unlock(&ipclock);
490 return err;
491}
492EXPORT_SYMBOL(intel_scu_ipc_command);
493
494
495#define IPC_I2C_WRITE 1
496#define IPC_I2C_READ 2
497
498
499
500
501
502
503
504
505
506
507
508
509
510int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
511{
512 struct intel_scu_ipc_dev *scu = &ipcdev;
513 u32 cmd = 0;
514
515 mutex_lock(&ipclock);
516 if (scu->dev == NULL) {
517 mutex_unlock(&ipclock);
518 return -ENODEV;
519 }
520 cmd = (addr >> 24) & 0xFF;
521 if (cmd == IPC_I2C_READ) {
522 writel(addr, scu->i2c_base + IPC_I2C_CNTRL_ADDR);
523
524 mdelay(1);
525 *data = readl(scu->i2c_base + I2C_DATA_ADDR);
526 } else if (cmd == IPC_I2C_WRITE) {
527 writel(*data, scu->i2c_base + I2C_DATA_ADDR);
528 mdelay(1);
529 writel(addr, scu->i2c_base + IPC_I2C_CNTRL_ADDR);
530 } else {
531 dev_err(scu->dev,
532 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
533
534 mutex_unlock(&ipclock);
535 return -EIO;
536 }
537 mutex_unlock(&ipclock);
538 return 0;
539}
540EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
541
542
543
544
545
546
547
548
549static irqreturn_t ioc(int irq, void *dev_id)
550{
551 struct intel_scu_ipc_dev *scu = dev_id;
552
553 if (scu->irq_mode)
554 complete(&scu->cmd_complete);
555
556 return IRQ_HANDLED;
557}
558
559
560
561
562
563
564
565
566
567static int ipc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
568{
569 int platform;
570 int err;
571 struct intel_scu_ipc_dev *scu = &ipcdev;
572 struct intel_scu_ipc_pdata_t *pdata;
573
574 platform = intel_mid_identify_cpu();
575 if (platform == 0)
576 return -ENODEV;
577
578 if (scu->dev)
579 return -EBUSY;
580
581 pdata = (struct intel_scu_ipc_pdata_t *)id->driver_data;
582
583 scu->dev = &pdev->dev;
584 scu->irq_mode = pdata->irq_mode;
585
586 err = pcim_enable_device(pdev);
587 if (err)
588 return err;
589
590 err = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
591 if (err)
592 return err;
593
594 init_completion(&scu->cmd_complete);
595
596 err = devm_request_irq(&pdev->dev, pdev->irq, ioc, 0, "intel_scu_ipc",
597 scu);
598 if (err)
599 return err;
600
601 scu->ipc_base = pcim_iomap_table(pdev)[0];
602
603 scu->i2c_base = ioremap_nocache(pdata->i2c_base, pdata->i2c_len);
604 if (!scu->i2c_base)
605 return -ENOMEM;
606
607 intel_scu_devices_create();
608
609 pci_set_drvdata(pdev, scu);
610 return 0;
611}
612
613static const struct pci_device_id pci_ids[] = {
614 {
615 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_LINCROFT),
616 (kernel_ulong_t)&intel_scu_ipc_lincroft_pdata,
617 }, {
618 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PENWELL),
619 (kernel_ulong_t)&intel_scu_ipc_penwell_pdata,
620 }, {
621 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_CLOVERVIEW),
622 (kernel_ulong_t)&intel_scu_ipc_penwell_pdata,
623 }, {
624 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_TANGIER),
625 (kernel_ulong_t)&intel_scu_ipc_tangier_pdata,
626 }, {
627 0,
628 }
629};
630
631static struct pci_driver ipc_driver = {
632 .driver = {
633 .suppress_bind_attrs = true,
634 },
635 .name = "intel_scu_ipc",
636 .id_table = pci_ids,
637 .probe = ipc_probe,
638};
639builtin_pci_driver(ipc_driver);
640