11da177e4SLinus Torvalds /* 2*d23eee88SFinn Thain * Device driver for the Cuda and Egret system controllers found on PowerMacs 3*d23eee88SFinn Thain * and 68k Macs. 41da177e4SLinus Torvalds * 5*d23eee88SFinn Thain * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA. 6*d23eee88SFinn Thain * This MCU controls system power, Parameter RAM, Real Time Clock and the 7*d23eee88SFinn Thain * Apple Desktop Bus (ADB) that connects to the keyboard and mouse. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Copyright (C) 1996 Paul Mackerras. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds #include <stdarg.h> 121da177e4SLinus Torvalds #include <linux/types.h> 131da177e4SLinus Torvalds #include <linux/errno.h> 141da177e4SLinus Torvalds #include <linux/kernel.h> 151da177e4SLinus Torvalds #include <linux/delay.h> 161da177e4SLinus Torvalds #include <linux/adb.h> 171da177e4SLinus Torvalds #include <linux/cuda.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 191da177e4SLinus Torvalds #include <linux/interrupt.h> 201da177e4SLinus Torvalds #ifdef CONFIG_PPC 211da177e4SLinus Torvalds #include <asm/prom.h> 221da177e4SLinus Torvalds #include <asm/machdep.h> 231da177e4SLinus Torvalds #else 241da177e4SLinus Torvalds #include <asm/macintosh.h> 251da177e4SLinus Torvalds #include <asm/macints.h> 261da177e4SLinus Torvalds #include <asm/mac_via.h> 271da177e4SLinus Torvalds #endif 281da177e4SLinus Torvalds #include <asm/io.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds static volatile unsigned char __iomem *via; 321da177e4SLinus Torvalds static DEFINE_SPINLOCK(cuda_lock); 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* VIA registers - spaced 0x200 bytes apart */ 351da177e4SLinus Torvalds #define RS 0x200 /* skip between registers */ 361da177e4SLinus Torvalds #define B 0 /* B-side data */ 371da177e4SLinus Torvalds #define A RS /* A-side data */ 381da177e4SLinus Torvalds #define DIRB (2*RS) /* B-side direction (1=output) */ 391da177e4SLinus Torvalds #define DIRA (3*RS) /* A-side direction (1=output) */ 401da177e4SLinus Torvalds #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */ 411da177e4SLinus Torvalds #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */ 421da177e4SLinus Torvalds #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */ 431da177e4SLinus Torvalds #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */ 441da177e4SLinus Torvalds #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */ 451da177e4SLinus Torvalds #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */ 461da177e4SLinus Torvalds #define SR (10*RS) /* Shift register */ 471da177e4SLinus Torvalds #define ACR (11*RS) /* Auxiliary control register */ 481da177e4SLinus Torvalds #define PCR (12*RS) /* Peripheral control register */ 491da177e4SLinus Torvalds #define IFR (13*RS) /* Interrupt flag register */ 501da177e4SLinus Torvalds #define IER (14*RS) /* Interrupt enable register */ 511da177e4SLinus Torvalds #define ANH (15*RS) /* A-side data, no handshake */ 521da177e4SLinus Torvalds 53*d23eee88SFinn Thain /* 54*d23eee88SFinn Thain * When the Cuda design replaced the Egret, some signal names and 55*d23eee88SFinn Thain * logic sense changed. They all serve the same purposes, however. 56*d23eee88SFinn Thain * 57*d23eee88SFinn Thain * VIA pin | Egret pin 58*d23eee88SFinn Thain * ----------------+------------------------------------------ 59*d23eee88SFinn Thain * PB3 (input) | Transceiver session (active low) 60*d23eee88SFinn Thain * PB4 (output) | VIA full (active high) 61*d23eee88SFinn Thain * PB5 (output) | System session (active high) 62*d23eee88SFinn Thain * 63*d23eee88SFinn Thain * VIA pin | Cuda pin 64*d23eee88SFinn Thain * ----------------+------------------------------------------ 65*d23eee88SFinn Thain * PB3 (input) | Transfer request (active low) 66*d23eee88SFinn Thain * PB4 (output) | Byte acknowledge (active low) 67*d23eee88SFinn Thain * PB5 (output) | Transfer in progress (active low) 68*d23eee88SFinn Thain */ 69*d23eee88SFinn Thain 70*d23eee88SFinn Thain /* Bits in Port B data register */ 71*d23eee88SFinn Thain #define TREQ 0x08 /* Transfer request */ 72*d23eee88SFinn Thain #define TACK 0x10 /* Transfer acknowledge */ 73*d23eee88SFinn Thain #define TIP 0x20 /* Transfer in progress */ 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* Bits in ACR */ 761da177e4SLinus Torvalds #define SR_CTRL 0x1c /* Shift register control bits */ 771da177e4SLinus Torvalds #define SR_EXT 0x0c /* Shift on external clock */ 781da177e4SLinus Torvalds #define SR_OUT 0x10 /* Shift out if 1 */ 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* Bits in IFR and IER */ 811da177e4SLinus Torvalds #define IER_SET 0x80 /* set bits in IER */ 821da177e4SLinus Torvalds #define IER_CLR 0 /* clear bits in IER */ 831da177e4SLinus Torvalds #define SR_INT 0x04 /* Shift register full/empty */ 841da177e4SLinus Torvalds 85*d23eee88SFinn Thain /* Duration of byte acknowledgement pulse (us) */ 86*d23eee88SFinn Thain #define EGRET_TACK_ASSERTED_DELAY 300 87*d23eee88SFinn Thain #define EGRET_TACK_NEGATED_DELAY 400 88*d23eee88SFinn Thain 89*d23eee88SFinn Thain /* Interval from interrupt to start of session (us) */ 90*d23eee88SFinn Thain #define EGRET_SESSION_DELAY 450 91*d23eee88SFinn Thain 92*d23eee88SFinn Thain #ifdef CONFIG_PPC 93*d23eee88SFinn Thain #define mcu_is_egret false 94*d23eee88SFinn Thain #else 95*d23eee88SFinn Thain static bool mcu_is_egret; 96*d23eee88SFinn Thain #endif 97*d23eee88SFinn Thain 98fd7a65a2SFinn Thain static inline bool TREQ_asserted(u8 portb) 99fd7a65a2SFinn Thain { 100fd7a65a2SFinn Thain return !(portb & TREQ); 101fd7a65a2SFinn Thain } 102fd7a65a2SFinn Thain 103fd7a65a2SFinn Thain static inline void assert_TIP(void) 104fd7a65a2SFinn Thain { 105*d23eee88SFinn Thain if (mcu_is_egret) { 106*d23eee88SFinn Thain udelay(EGRET_SESSION_DELAY); 107*d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) | TIP); 108*d23eee88SFinn Thain } else 109fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) & ~TIP); 110fd7a65a2SFinn Thain } 111fd7a65a2SFinn Thain 112*d23eee88SFinn Thain static inline void assert_TIP_and_TACK(void) 113*d23eee88SFinn Thain { 114*d23eee88SFinn Thain if (mcu_is_egret) { 115*d23eee88SFinn Thain udelay(EGRET_SESSION_DELAY); 116*d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) | TIP | TACK); 117*d23eee88SFinn Thain } else 118*d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK)); 119*d23eee88SFinn Thain } 120*d23eee88SFinn Thain 121fd7a65a2SFinn Thain static inline void assert_TACK(void) 122fd7a65a2SFinn Thain { 123*d23eee88SFinn Thain if (mcu_is_egret) { 124*d23eee88SFinn Thain udelay(EGRET_TACK_NEGATED_DELAY); 125*d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) | TACK); 126*d23eee88SFinn Thain } else 127fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) & ~TACK); 128fd7a65a2SFinn Thain } 129fd7a65a2SFinn Thain 130fd7a65a2SFinn Thain static inline void toggle_TACK(void) 131fd7a65a2SFinn Thain { 132fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) ^ TACK); 133fd7a65a2SFinn Thain } 134fd7a65a2SFinn Thain 135fd7a65a2SFinn Thain static inline void negate_TACK(void) 136fd7a65a2SFinn Thain { 137*d23eee88SFinn Thain if (mcu_is_egret) { 138*d23eee88SFinn Thain udelay(EGRET_TACK_ASSERTED_DELAY); 139*d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) & ~TACK); 140*d23eee88SFinn Thain } else 141fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) | TACK); 142fd7a65a2SFinn Thain } 143fd7a65a2SFinn Thain 144fd7a65a2SFinn Thain static inline void negate_TIP_and_TACK(void) 145fd7a65a2SFinn Thain { 146*d23eee88SFinn Thain if (mcu_is_egret) { 147*d23eee88SFinn Thain udelay(EGRET_TACK_ASSERTED_DELAY); 148*d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK)); 149*d23eee88SFinn Thain } else 150fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) | TIP | TACK); 151fd7a65a2SFinn Thain } 152fd7a65a2SFinn Thain 1531da177e4SLinus Torvalds static enum cuda_state { 1541da177e4SLinus Torvalds idle, 1551da177e4SLinus Torvalds sent_first_byte, 1561da177e4SLinus Torvalds sending, 1571da177e4SLinus Torvalds reading, 1581da177e4SLinus Torvalds read_done, 1591da177e4SLinus Torvalds awaiting_reply 1601da177e4SLinus Torvalds } cuda_state; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds static struct adb_request *current_req; 1631da177e4SLinus Torvalds static struct adb_request *last_req; 1641da177e4SLinus Torvalds static unsigned char cuda_rbuf[16]; 1651da177e4SLinus Torvalds static unsigned char *reply_ptr; 1661da177e4SLinus Torvalds static int reading_reply; 1671da177e4SLinus Torvalds static int data_index; 1680251c38cSFinn Thain static int cuda_irq; 1691da177e4SLinus Torvalds #ifdef CONFIG_PPC 1701da177e4SLinus Torvalds static struct device_node *vias; 1711da177e4SLinus Torvalds #endif 17287275856SOlaf Hering static int cuda_fully_inited; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds #ifdef CONFIG_ADB 1751da177e4SLinus Torvalds static int cuda_probe(void); 1761da177e4SLinus Torvalds static int cuda_send_request(struct adb_request *req, int sync); 1771da177e4SLinus Torvalds static int cuda_adb_autopoll(int devs); 1781da177e4SLinus Torvalds static int cuda_reset_adb_bus(void); 1791da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static int cuda_init_via(void); 1821da177e4SLinus Torvalds static void cuda_start(void); 1837d12e780SDavid Howells static irqreturn_t cuda_interrupt(int irq, void *arg); 1847d12e780SDavid Howells static void cuda_input(unsigned char *buf, int nb); 1851da177e4SLinus Torvalds void cuda_poll(void); 1861da177e4SLinus Torvalds static int cuda_write(struct adb_request *req); 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds int cuda_request(struct adb_request *req, 1891da177e4SLinus Torvalds void (*done)(struct adb_request *), int nbytes, ...); 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds #ifdef CONFIG_ADB 1921da177e4SLinus Torvalds struct adb_driver via_cuda_driver = { 19318814ee8SFinn Thain .name = "CUDA", 19418814ee8SFinn Thain .probe = cuda_probe, 19518814ee8SFinn Thain .send_request = cuda_send_request, 19618814ee8SFinn Thain .autopoll = cuda_adb_autopoll, 19718814ee8SFinn Thain .poll = cuda_poll, 19818814ee8SFinn Thain .reset_bus = cuda_reset_adb_bus, 1991da177e4SLinus Torvalds }; 2001da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 2011da177e4SLinus Torvalds 20218814ee8SFinn Thain #ifdef CONFIG_MAC 20318814ee8SFinn Thain int __init find_via_cuda(void) 20418814ee8SFinn Thain { 20518814ee8SFinn Thain struct adb_request req; 20618814ee8SFinn Thain int err; 20718814ee8SFinn Thain 20818814ee8SFinn Thain if (macintosh_config->adb_type != MAC_ADB_CUDA) 20918814ee8SFinn Thain return 0; 21018814ee8SFinn Thain 21118814ee8SFinn Thain via = via1; 21218814ee8SFinn Thain cuda_state = idle; 213*d23eee88SFinn Thain mcu_is_egret = false; 21418814ee8SFinn Thain 21518814ee8SFinn Thain err = cuda_init_via(); 21618814ee8SFinn Thain if (err) { 21718814ee8SFinn Thain printk(KERN_ERR "cuda_init_via() failed\n"); 21818814ee8SFinn Thain via = NULL; 21918814ee8SFinn Thain return 0; 22018814ee8SFinn Thain } 22118814ee8SFinn Thain 22218814ee8SFinn Thain /* enable autopoll */ 22318814ee8SFinn Thain cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); 22418814ee8SFinn Thain while (!req.complete) 22518814ee8SFinn Thain cuda_poll(); 22618814ee8SFinn Thain 22718814ee8SFinn Thain return 1; 22818814ee8SFinn Thain } 22918814ee8SFinn Thain #else 23051d3082fSBenjamin Herrenschmidt int __init find_via_cuda(void) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds struct adb_request req; 23351d3082fSBenjamin Herrenschmidt phys_addr_t taddr; 234018a3d1dSJeremy Kerr const u32 *reg; 23551d3082fSBenjamin Herrenschmidt int err; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (vias != 0) 2381da177e4SLinus Torvalds return 1; 23951d3082fSBenjamin Herrenschmidt vias = of_find_node_by_name(NULL, "via-cuda"); 2401da177e4SLinus Torvalds if (vias == 0) 2411da177e4SLinus Torvalds return 0; 2421da177e4SLinus Torvalds 24301b2726dSStephen Rothwell reg = of_get_property(vias, "reg", NULL); 24451d3082fSBenjamin Herrenschmidt if (reg == NULL) { 24551d3082fSBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: No \"reg\" property !\n"); 24651d3082fSBenjamin Herrenschmidt goto fail; 2471da177e4SLinus Torvalds } 24851d3082fSBenjamin Herrenschmidt taddr = of_translate_address(vias, reg); 24951d3082fSBenjamin Herrenschmidt if (taddr == 0) { 25051d3082fSBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: Can't translate address !\n"); 25151d3082fSBenjamin Herrenschmidt goto fail; 25251d3082fSBenjamin Herrenschmidt } 25351d3082fSBenjamin Herrenschmidt via = ioremap(taddr, 0x2000); 25451d3082fSBenjamin Herrenschmidt if (via == NULL) { 25551d3082fSBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: Can't map address !\n"); 25651d3082fSBenjamin Herrenschmidt goto fail; 25751d3082fSBenjamin Herrenschmidt } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds cuda_state = idle; 2601da177e4SLinus Torvalds sys_ctrler = SYS_CTRLER_CUDA; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds err = cuda_init_via(); 2631da177e4SLinus Torvalds if (err) { 2641da177e4SLinus Torvalds printk(KERN_ERR "cuda_init_via() failed\n"); 2651da177e4SLinus Torvalds via = NULL; 2661da177e4SLinus Torvalds return 0; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* Clear and enable interrupts, but only on PPC. On 68K it's done */ 2701da177e4SLinus Torvalds /* for us by the main VIA driver in arch/m68k/mac/via.c */ 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */ 2731da177e4SLinus Torvalds out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */ 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* enable autopoll */ 2761da177e4SLinus Torvalds cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); 2771da177e4SLinus Torvalds while (!req.complete) 2781da177e4SLinus Torvalds cuda_poll(); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds return 1; 28151d3082fSBenjamin Herrenschmidt 28251d3082fSBenjamin Herrenschmidt fail: 28351d3082fSBenjamin Herrenschmidt of_node_put(vias); 28451d3082fSBenjamin Herrenschmidt vias = NULL; 28551d3082fSBenjamin Herrenschmidt return 0; 2861da177e4SLinus Torvalds } 28718814ee8SFinn Thain #endif /* !defined CONFIG_MAC */ 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static int __init via_cuda_start(void) 2901da177e4SLinus Torvalds { 2911da177e4SLinus Torvalds if (via == NULL) 2921da177e4SLinus Torvalds return -ENODEV; 2931da177e4SLinus Torvalds 2940ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_MAC 2950251c38cSFinn Thain cuda_irq = IRQ_MAC_ADB; 29618814ee8SFinn Thain #else 2970251c38cSFinn Thain cuda_irq = irq_of_parse_and_map(vias, 0); 298ef24ba70SMichael Ellerman if (!cuda_irq) { 2990ebfff14SBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: can't map interrupts for %s\n", 3000ebfff14SBenjamin Herrenschmidt vias->full_name); 3010ebfff14SBenjamin Herrenschmidt return -ENODEV; 3020ebfff14SBenjamin Herrenschmidt } 30318814ee8SFinn Thain #endif 3040ebfff14SBenjamin Herrenschmidt 3050251c38cSFinn Thain if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) { 3060251c38cSFinn Thain printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq); 3071da177e4SLinus Torvalds return -EAGAIN; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 310*d23eee88SFinn Thain pr_info("Macintosh Cuda and Egret driver.\n"); 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds cuda_fully_inited = 1; 3131da177e4SLinus Torvalds return 0; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds device_initcall(via_cuda_start); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds #ifdef CONFIG_ADB 3191da177e4SLinus Torvalds static int 3201da177e4SLinus Torvalds cuda_probe(void) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds #ifdef CONFIG_PPC 3231da177e4SLinus Torvalds if (sys_ctrler != SYS_CTRLER_CUDA) 3241da177e4SLinus Torvalds return -ENODEV; 3251da177e4SLinus Torvalds #else 3261da177e4SLinus Torvalds if (macintosh_config->adb_type != MAC_ADB_CUDA) 3271da177e4SLinus Torvalds return -ENODEV; 3281da177e4SLinus Torvalds #endif 3291da177e4SLinus Torvalds if (via == NULL) 3301da177e4SLinus Torvalds return -ENODEV; 3311da177e4SLinus Torvalds return 0; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 3341da177e4SLinus Torvalds 335*d23eee88SFinn Thain static int __init sync_egret(void) 336*d23eee88SFinn Thain { 337*d23eee88SFinn Thain if (TREQ_asserted(in_8(&via[B]))) { 338*d23eee88SFinn Thain /* Complete the inbound transfer */ 339*d23eee88SFinn Thain assert_TIP_and_TACK(); 340*d23eee88SFinn Thain while (1) { 341*d23eee88SFinn Thain negate_TACK(); 342*d23eee88SFinn Thain mdelay(1); 343*d23eee88SFinn Thain (void)in_8(&via[SR]); 344*d23eee88SFinn Thain assert_TACK(); 345*d23eee88SFinn Thain if (!TREQ_asserted(in_8(&via[B]))) 346*d23eee88SFinn Thain break; 347*d23eee88SFinn Thain } 348*d23eee88SFinn Thain negate_TIP_and_TACK(); 349*d23eee88SFinn Thain } else if (in_8(&via[B]) & TIP) { 350*d23eee88SFinn Thain /* Terminate the outbound transfer */ 351*d23eee88SFinn Thain negate_TACK(); 352*d23eee88SFinn Thain assert_TACK(); 353*d23eee88SFinn Thain mdelay(1); 354*d23eee88SFinn Thain negate_TIP_and_TACK(); 355*d23eee88SFinn Thain } 356*d23eee88SFinn Thain /* Clear shift register interrupt */ 357*d23eee88SFinn Thain if (in_8(&via[IFR]) & SR_INT) 358*d23eee88SFinn Thain (void)in_8(&via[SR]); 359*d23eee88SFinn Thain return 0; 360*d23eee88SFinn Thain } 361*d23eee88SFinn Thain 3621da177e4SLinus Torvalds #define WAIT_FOR(cond, what) \ 3631da177e4SLinus Torvalds do { \ 3641da177e4SLinus Torvalds int x; \ 3651da177e4SLinus Torvalds for (x = 1000; !(cond); --x) { \ 3661da177e4SLinus Torvalds if (x == 0) { \ 367523717d1SFinn Thain pr_err("Timeout waiting for " what "\n"); \ 3681da177e4SLinus Torvalds return -ENXIO; \ 3691da177e4SLinus Torvalds } \ 3701da177e4SLinus Torvalds udelay(100); \ 3711da177e4SLinus Torvalds } \ 3721da177e4SLinus Torvalds } while (0) 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds static int 375330dae19SGeert Uytterhoeven __init cuda_init_via(void) 3761da177e4SLinus Torvalds { 3770251c38cSFinn Thain #ifdef CONFIG_PPC 3781da177e4SLinus Torvalds out_8(&via[IER], 0x7f); /* disable interrupts from VIA */ 3791da177e4SLinus Torvalds (void)in_8(&via[IER]); 3800251c38cSFinn Thain #else 3810251c38cSFinn Thain out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */ 3821da177e4SLinus Torvalds #endif 3831da177e4SLinus Torvalds 384*d23eee88SFinn Thain out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ 385*d23eee88SFinn Thain out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ 386*d23eee88SFinn Thain (void)in_8(&via[SR]); /* clear any left-over data */ 387*d23eee88SFinn Thain 388*d23eee88SFinn Thain if (mcu_is_egret) 389*d23eee88SFinn Thain return sync_egret(); 390*d23eee88SFinn Thain 391*d23eee88SFinn Thain negate_TIP_and_TACK(); 392*d23eee88SFinn Thain 3931da177e4SLinus Torvalds /* delay 4ms and then clear any pending interrupt */ 3941da177e4SLinus Torvalds mdelay(4); 3951da177e4SLinus Torvalds (void)in_8(&via[SR]); 3960251c38cSFinn Thain out_8(&via[IFR], SR_INT); 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* sync with the CUDA - assert TACK without TIP */ 399fd7a65a2SFinn Thain assert_TACK(); 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* wait for the CUDA to assert TREQ in response */ 402fd7a65a2SFinn Thain WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync"); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds /* wait for the interrupt and then clear it */ 4051da177e4SLinus Torvalds WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)"); 4061da177e4SLinus Torvalds (void)in_8(&via[SR]); 4070251c38cSFinn Thain out_8(&via[IFR], SR_INT); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* finish the sync by negating TACK */ 410fd7a65a2SFinn Thain negate_TACK(); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* wait for the CUDA to negate TREQ and the corresponding interrupt */ 413fd7a65a2SFinn Thain WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)"); 4141da177e4SLinus Torvalds WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)"); 4151da177e4SLinus Torvalds (void)in_8(&via[SR]); 4160251c38cSFinn Thain out_8(&via[IFR], SR_INT); 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds return 0; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds #ifdef CONFIG_ADB 4221da177e4SLinus Torvalds /* Send an ADB command */ 4231da177e4SLinus Torvalds static int 4241da177e4SLinus Torvalds cuda_send_request(struct adb_request *req, int sync) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds int i; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds if ((via == NULL) || !cuda_fully_inited) { 4291da177e4SLinus Torvalds req->complete = 1; 4301da177e4SLinus Torvalds return -ENXIO; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds req->reply_expected = 1; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds i = cuda_write(req); 4361da177e4SLinus Torvalds if (i) 4371da177e4SLinus Torvalds return i; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (sync) { 4401da177e4SLinus Torvalds while (!req->complete) 4411da177e4SLinus Torvalds cuda_poll(); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds return 0; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* Enable/disable autopolling */ 4481da177e4SLinus Torvalds static int 4491da177e4SLinus Torvalds cuda_adb_autopoll(int devs) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds struct adb_request req; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds if ((via == NULL) || !cuda_fully_inited) 4541da177e4SLinus Torvalds return -ENXIO; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0)); 4571da177e4SLinus Torvalds while (!req.complete) 4581da177e4SLinus Torvalds cuda_poll(); 4591da177e4SLinus Torvalds return 0; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* Reset adb bus - how do we do this?? */ 4631da177e4SLinus Torvalds static int 4641da177e4SLinus Torvalds cuda_reset_adb_bus(void) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct adb_request req; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds if ((via == NULL) || !cuda_fully_inited) 4691da177e4SLinus Torvalds return -ENXIO; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */ 4721da177e4SLinus Torvalds while (!req.complete) 4731da177e4SLinus Torvalds cuda_poll(); 4741da177e4SLinus Torvalds return 0; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 477523717d1SFinn Thain 4781da177e4SLinus Torvalds /* Construct and send a cuda request */ 4791da177e4SLinus Torvalds int 4801da177e4SLinus Torvalds cuda_request(struct adb_request *req, void (*done)(struct adb_request *), 4811da177e4SLinus Torvalds int nbytes, ...) 4821da177e4SLinus Torvalds { 4831da177e4SLinus Torvalds va_list list; 4841da177e4SLinus Torvalds int i; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds if (via == NULL) { 4871da177e4SLinus Torvalds req->complete = 1; 4881da177e4SLinus Torvalds return -ENXIO; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds req->nbytes = nbytes; 4921da177e4SLinus Torvalds req->done = done; 4931da177e4SLinus Torvalds va_start(list, nbytes); 4941da177e4SLinus Torvalds for (i = 0; i < nbytes; ++i) 4951da177e4SLinus Torvalds req->data[i] = va_arg(list, int); 4961da177e4SLinus Torvalds va_end(list); 4971da177e4SLinus Torvalds req->reply_expected = 1; 4981da177e4SLinus Torvalds return cuda_write(req); 4991da177e4SLinus Torvalds } 5004a1b08e8SAnton Blanchard EXPORT_SYMBOL(cuda_request); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds static int 5031da177e4SLinus Torvalds cuda_write(struct adb_request *req) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds unsigned long flags; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) { 5081da177e4SLinus Torvalds req->complete = 1; 5091da177e4SLinus Torvalds return -EINVAL; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds req->next = NULL; 5121da177e4SLinus Torvalds req->sent = 0; 5131da177e4SLinus Torvalds req->complete = 0; 5141da177e4SLinus Torvalds req->reply_len = 0; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds spin_lock_irqsave(&cuda_lock, flags); 5171da177e4SLinus Torvalds if (current_req != 0) { 5181da177e4SLinus Torvalds last_req->next = req; 5191da177e4SLinus Torvalds last_req = req; 5201da177e4SLinus Torvalds } else { 5211da177e4SLinus Torvalds current_req = req; 5221da177e4SLinus Torvalds last_req = req; 5231da177e4SLinus Torvalds if (cuda_state == idle) 5241da177e4SLinus Torvalds cuda_start(); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds spin_unlock_irqrestore(&cuda_lock, flags); 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds return 0; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds static void 5321da177e4SLinus Torvalds cuda_start(void) 5331da177e4SLinus Torvalds { 5341da177e4SLinus Torvalds /* assert cuda_state == idle */ 53506d7e994SFinn Thain if (current_req == NULL) 5361da177e4SLinus Torvalds return; 53797ced1aaSFinn Thain data_index = 0; 538fd7a65a2SFinn Thain if (TREQ_asserted(in_8(&via[B]))) 5391da177e4SLinus Torvalds return; /* a byte is coming in from the CUDA */ 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds /* set the shift register to shift out and send a byte */ 5421da177e4SLinus Torvalds out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); 54397ced1aaSFinn Thain out_8(&via[SR], current_req->data[data_index++]); 544*d23eee88SFinn Thain if (mcu_is_egret) 545*d23eee88SFinn Thain assert_TIP_and_TACK(); 546*d23eee88SFinn Thain else 547fd7a65a2SFinn Thain assert_TIP(); 5481da177e4SLinus Torvalds cuda_state = sent_first_byte; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds void 5521da177e4SLinus Torvalds cuda_poll(void) 5531da177e4SLinus Torvalds { 55449f19ce4SOlof Johansson cuda_interrupt(0, NULL); 5551da177e4SLinus Torvalds } 5564a1b08e8SAnton Blanchard EXPORT_SYMBOL(cuda_poll); 5571da177e4SLinus Torvalds 558fe73b582SFinn Thain #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a)) 559fe73b582SFinn Thain 5601da177e4SLinus Torvalds static irqreturn_t 5617d12e780SDavid Howells cuda_interrupt(int irq, void *arg) 5621da177e4SLinus Torvalds { 563ac39452eSFinn Thain unsigned long flags; 564fd7a65a2SFinn Thain u8 status; 5651da177e4SLinus Torvalds struct adb_request *req = NULL; 5661da177e4SLinus Torvalds unsigned char ibuf[16]; 5671da177e4SLinus Torvalds int ibuf_len = 0; 5681da177e4SLinus Torvalds int complete = 0; 5691da177e4SLinus Torvalds 570ac39452eSFinn Thain spin_lock_irqsave(&cuda_lock, flags); 5711da177e4SLinus Torvalds 57218814ee8SFinn Thain /* On powermacs, this handler is registered for the VIA IRQ. But they use 5730251c38cSFinn Thain * just the shift register IRQ -- other VIA interrupt sources are disabled. 5740251c38cSFinn Thain * On m68k macs, the VIA IRQ sources are dispatched individually. Unless 5750251c38cSFinn Thain * we are polling, the shift register IRQ flag has already been cleared. 5760251c38cSFinn Thain */ 5770251c38cSFinn Thain 5780251c38cSFinn Thain #ifdef CONFIG_MAC 5790251c38cSFinn Thain if (!arg) 5800251c38cSFinn Thain #endif 5810251c38cSFinn Thain { 5820251c38cSFinn Thain if ((in_8(&via[IFR]) & SR_INT) == 0) { 583ac39452eSFinn Thain spin_unlock_irqrestore(&cuda_lock, flags); 5841da177e4SLinus Torvalds return IRQ_NONE; 5850251c38cSFinn Thain } else { 5860251c38cSFinn Thain out_8(&via[IFR], SR_INT); 5870251c38cSFinn Thain } 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 590fd7a65a2SFinn Thain status = in_8(&via[B]) & (TIP | TACK | TREQ); 591fd7a65a2SFinn Thain 5921da177e4SLinus Torvalds switch (cuda_state) { 5931da177e4SLinus Torvalds case idle: 594*d23eee88SFinn Thain /* System controller has unsolicited data for us */ 5951da177e4SLinus Torvalds (void)in_8(&via[SR]); 596*d23eee88SFinn Thain idle_state: 597fd7a65a2SFinn Thain assert_TIP(); 5981da177e4SLinus Torvalds cuda_state = reading; 5991da177e4SLinus Torvalds reply_ptr = cuda_rbuf; 6001da177e4SLinus Torvalds reading_reply = 0; 6011da177e4SLinus Torvalds break; 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds case awaiting_reply: 604*d23eee88SFinn Thain /* System controller has reply data for us */ 6051da177e4SLinus Torvalds (void)in_8(&via[SR]); 606fd7a65a2SFinn Thain assert_TIP(); 6071da177e4SLinus Torvalds cuda_state = reading; 6081da177e4SLinus Torvalds reply_ptr = current_req->reply; 6091da177e4SLinus Torvalds reading_reply = 1; 6101da177e4SLinus Torvalds break; 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds case sent_first_byte: 613fd7a65a2SFinn Thain if (TREQ_asserted(status)) { 6141da177e4SLinus Torvalds /* collision */ 6151da177e4SLinus Torvalds out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); 6161da177e4SLinus Torvalds (void)in_8(&via[SR]); 617fd7a65a2SFinn Thain negate_TIP_and_TACK(); 6181da177e4SLinus Torvalds cuda_state = idle; 619*d23eee88SFinn Thain /* Egret does not raise an "aborted" interrupt */ 620*d23eee88SFinn Thain if (mcu_is_egret) 621*d23eee88SFinn Thain goto idle_state; 6221da177e4SLinus Torvalds } else { 62397ced1aaSFinn Thain out_8(&via[SR], current_req->data[data_index++]); 624fd7a65a2SFinn Thain toggle_TACK(); 625*d23eee88SFinn Thain if (mcu_is_egret) 626*d23eee88SFinn Thain assert_TACK(); 6271da177e4SLinus Torvalds cuda_state = sending; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds break; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds case sending: 6321da177e4SLinus Torvalds req = current_req; 6331da177e4SLinus Torvalds if (data_index >= req->nbytes) { 6341da177e4SLinus Torvalds out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); 6351da177e4SLinus Torvalds (void)in_8(&via[SR]); 636fd7a65a2SFinn Thain negate_TIP_and_TACK(); 6371da177e4SLinus Torvalds req->sent = 1; 6381da177e4SLinus Torvalds if (req->reply_expected) { 6391da177e4SLinus Torvalds cuda_state = awaiting_reply; 6401da177e4SLinus Torvalds } else { 6411da177e4SLinus Torvalds current_req = req->next; 6421da177e4SLinus Torvalds complete = 1; 6431da177e4SLinus Torvalds /* not sure about this */ 6441da177e4SLinus Torvalds cuda_state = idle; 6451da177e4SLinus Torvalds cuda_start(); 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds } else { 6481da177e4SLinus Torvalds out_8(&via[SR], req->data[data_index++]); 649fd7a65a2SFinn Thain toggle_TACK(); 650*d23eee88SFinn Thain if (mcu_is_egret) 651*d23eee88SFinn Thain assert_TACK(); 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds break; 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds case reading: 656fe73b582SFinn Thain if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr) 657fe73b582SFinn Thain : ARRAY_FULL(cuda_rbuf, reply_ptr)) 658fe73b582SFinn Thain (void)in_8(&via[SR]); 659fe73b582SFinn Thain else 6601da177e4SLinus Torvalds *reply_ptr++ = in_8(&via[SR]); 661fd7a65a2SFinn Thain if (!TREQ_asserted(status)) { 662*d23eee88SFinn Thain if (mcu_is_egret) 663*d23eee88SFinn Thain assert_TACK(); 6641da177e4SLinus Torvalds /* that's all folks */ 665fd7a65a2SFinn Thain negate_TIP_and_TACK(); 6661da177e4SLinus Torvalds cuda_state = read_done; 667*d23eee88SFinn Thain /* Egret does not raise a "read done" interrupt */ 668*d23eee88SFinn Thain if (mcu_is_egret) 669*d23eee88SFinn Thain goto read_done_state; 6701da177e4SLinus Torvalds } else { 671fd7a65a2SFinn Thain toggle_TACK(); 672*d23eee88SFinn Thain if (mcu_is_egret) 673*d23eee88SFinn Thain negate_TACK(); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds break; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds case read_done: 6781da177e4SLinus Torvalds (void)in_8(&via[SR]); 679*d23eee88SFinn Thain read_done_state: 6801da177e4SLinus Torvalds if (reading_reply) { 6811da177e4SLinus Torvalds req = current_req; 6821da177e4SLinus Torvalds req->reply_len = reply_ptr - req->reply; 6831da177e4SLinus Torvalds if (req->data[0] == ADB_PACKET) { 6841da177e4SLinus Torvalds /* Have to adjust the reply from ADB commands */ 6851da177e4SLinus Torvalds if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) { 6861da177e4SLinus Torvalds /* the 0x2 bit indicates no response */ 6871da177e4SLinus Torvalds req->reply_len = 0; 6881da177e4SLinus Torvalds } else { 6891da177e4SLinus Torvalds /* leave just the command and result bytes in the reply */ 6901da177e4SLinus Torvalds req->reply_len -= 2; 6911da177e4SLinus Torvalds memmove(req->reply, req->reply + 2, req->reply_len); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds current_req = req->next; 6951da177e4SLinus Torvalds complete = 1; 696cfbf9980SFinn Thain reading_reply = 0; 6971da177e4SLinus Torvalds } else { 6981da177e4SLinus Torvalds /* This is tricky. We must break the spinlock to call 6991da177e4SLinus Torvalds * cuda_input. However, doing so means we might get 7001da177e4SLinus Torvalds * re-entered from another CPU getting an interrupt 7011da177e4SLinus Torvalds * or calling cuda_poll(). I ended up using the stack 7021da177e4SLinus Torvalds * (it's only for 16 bytes) and moving the actual 7031da177e4SLinus Torvalds * call to cuda_input to outside of the lock. 7041da177e4SLinus Torvalds */ 7051da177e4SLinus Torvalds ibuf_len = reply_ptr - cuda_rbuf; 7061da177e4SLinus Torvalds memcpy(ibuf, cuda_rbuf, ibuf_len); 7071da177e4SLinus Torvalds } 708cfbf9980SFinn Thain reply_ptr = cuda_rbuf; 7091da177e4SLinus Torvalds cuda_state = idle; 7101da177e4SLinus Torvalds cuda_start(); 711a6466243SFinn Thain if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) { 712a6466243SFinn Thain assert_TIP(); 713a6466243SFinn Thain cuda_state = reading; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds break; 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds default: 718523717d1SFinn Thain pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); 7191da177e4SLinus Torvalds } 720ac39452eSFinn Thain spin_unlock_irqrestore(&cuda_lock, flags); 7211da177e4SLinus Torvalds if (complete && req) { 7221da177e4SLinus Torvalds void (*done)(struct adb_request *) = req->done; 7231da177e4SLinus Torvalds mb(); 7241da177e4SLinus Torvalds req->complete = 1; 7251da177e4SLinus Torvalds /* Here, we assume that if the request has a done member, the 7261da177e4SLinus Torvalds * struct request will survive to setting req->complete to 1 7271da177e4SLinus Torvalds */ 7281da177e4SLinus Torvalds if (done) 7291da177e4SLinus Torvalds (*done)(req); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds if (ibuf_len) 7327d12e780SDavid Howells cuda_input(ibuf, ibuf_len); 7331da177e4SLinus Torvalds return IRQ_HANDLED; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds static void 7377d12e780SDavid Howells cuda_input(unsigned char *buf, int nb) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds switch (buf[0]) { 7401da177e4SLinus Torvalds case ADB_PACKET: 7411da177e4SLinus Torvalds #ifdef CONFIG_XMON 7421da177e4SLinus Torvalds if (nb == 5 && buf[2] == 0x2c) { 7431da177e4SLinus Torvalds extern int xmon_wants_key, xmon_adb_keycode; 7441da177e4SLinus Torvalds if (xmon_wants_key) { 7451da177e4SLinus Torvalds xmon_adb_keycode = buf[3]; 7461da177e4SLinus Torvalds return; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds #endif /* CONFIG_XMON */ 7501da177e4SLinus Torvalds #ifdef CONFIG_ADB 7517d12e780SDavid Howells adb_input(buf+2, nb-2, buf[1] & 0x40); 7521da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 7531da177e4SLinus Torvalds break; 7541da177e4SLinus Torvalds 755*d23eee88SFinn Thain case TIMER_PACKET: 756*d23eee88SFinn Thain /* Egret sends these periodically. Might be useful as a 'heartbeat' 757*d23eee88SFinn Thain * to trigger a recovery for the VIA shift register errata. 758*d23eee88SFinn Thain */ 759*d23eee88SFinn Thain break; 760*d23eee88SFinn Thain 7611da177e4SLinus Torvalds default: 762523717d1SFinn Thain print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1, 763523717d1SFinn Thain buf, nb, false); 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds } 766