Path: blob/master/arch/powerpc/platforms/powermac/low_i2c.c
10818 views
/*1* arch/powerpc/platforms/powermac/low_i2c.c2*3* Copyright (C) 2003-2005 Ben. Herrenschmidt ([email protected])4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public License7* as published by the Free Software Foundation; either version8* 2 of the License, or (at your option) any later version.9*10* The linux i2c layer isn't completely suitable for our needs for various11* reasons ranging from too late initialisation to semantics not perfectly12* matching some requirements of the apple platform functions etc...13*14* This file thus provides a simple low level unified i2c interface for15* powermac that covers the various types of i2c busses used in Apple machines.16* For now, keywest, PMU and SMU, though we could add Cuda, or other bit17* banging busses found on older chipstes in earlier machines if we ever need18* one of them.19*20* The drivers in this file are synchronous/blocking. In addition, the21* keywest one is fairly slow due to the use of msleep instead of interrupts22* as the interrupt is currently used by i2c-keywest. In the long run, we23* might want to get rid of those high-level interfaces to linux i2c layer24* either completely (converting all drivers) or replacing them all with a25* single stub driver on top of this one. Once done, the interrupt will be26* available for our use.27*/2829#undef DEBUG30#undef DEBUG_LOW3132#include <linux/types.h>33#include <linux/sched.h>34#include <linux/init.h>35#include <linux/module.h>36#include <linux/adb.h>37#include <linux/pmu.h>38#include <linux/delay.h>39#include <linux/completion.h>40#include <linux/platform_device.h>41#include <linux/interrupt.h>42#include <linux/timer.h>43#include <linux/mutex.h>44#include <linux/i2c.h>45#include <linux/slab.h>46#include <asm/keylargo.h>47#include <asm/uninorth.h>48#include <asm/io.h>49#include <asm/prom.h>50#include <asm/machdep.h>51#include <asm/smu.h>52#include <asm/pmac_pfunc.h>53#include <asm/pmac_low_i2c.h>5455#ifdef DEBUG56#define DBG(x...) do {\57printk(KERN_DEBUG "low_i2c:" x); \58} while(0)59#else60#define DBG(x...)61#endif6263#ifdef DEBUG_LOW64#define DBG_LOW(x...) do {\65printk(KERN_DEBUG "low_i2c:" x); \66} while(0)67#else68#define DBG_LOW(x...)69#endif707172static int pmac_i2c_force_poll = 1;7374/*75* A bus structure. Each bus in the system has such a structure associated.76*/77struct pmac_i2c_bus78{79struct list_head link;80struct device_node *controller;81struct device_node *busnode;82int type;83int flags;84struct i2c_adapter adapter;85void *hostdata;86int channel; /* some hosts have multiple */87int mode; /* current mode */88struct mutex mutex;89int opened;90int polled; /* open mode */91struct platform_device *platform_dev;9293/* ops */94int (*open)(struct pmac_i2c_bus *bus);95void (*close)(struct pmac_i2c_bus *bus);96int (*xfer)(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,97u32 subaddr, u8 *data, int len);98};99100static LIST_HEAD(pmac_i2c_busses);101102/*103* Keywest implementation104*/105106struct pmac_i2c_host_kw107{108struct mutex mutex; /* Access mutex for use by109* i2c-keywest */110void __iomem *base; /* register base address */111int bsteps; /* register stepping */112int speed; /* speed */113int irq;114u8 *data;115unsigned len;116int state;117int rw;118int polled;119int result;120struct completion complete;121spinlock_t lock;122struct timer_list timeout_timer;123};124125/* Register indices */126typedef enum {127reg_mode = 0,128reg_control,129reg_status,130reg_isr,131reg_ier,132reg_addr,133reg_subaddr,134reg_data135} reg_t;136137/* The Tumbler audio equalizer can be really slow sometimes */138#define KW_POLL_TIMEOUT (2*HZ)139140/* Mode register */141#define KW_I2C_MODE_100KHZ 0x00142#define KW_I2C_MODE_50KHZ 0x01143#define KW_I2C_MODE_25KHZ 0x02144#define KW_I2C_MODE_DUMB 0x00145#define KW_I2C_MODE_STANDARD 0x04146#define KW_I2C_MODE_STANDARDSUB 0x08147#define KW_I2C_MODE_COMBINED 0x0C148#define KW_I2C_MODE_MODE_MASK 0x0C149#define KW_I2C_MODE_CHAN_MASK 0xF0150151/* Control register */152#define KW_I2C_CTL_AAK 0x01153#define KW_I2C_CTL_XADDR 0x02154#define KW_I2C_CTL_STOP 0x04155#define KW_I2C_CTL_START 0x08156157/* Status register */158#define KW_I2C_STAT_BUSY 0x01159#define KW_I2C_STAT_LAST_AAK 0x02160#define KW_I2C_STAT_LAST_RW 0x04161#define KW_I2C_STAT_SDA 0x08162#define KW_I2C_STAT_SCL 0x10163164/* IER & ISR registers */165#define KW_I2C_IRQ_DATA 0x01166#define KW_I2C_IRQ_ADDR 0x02167#define KW_I2C_IRQ_STOP 0x04168#define KW_I2C_IRQ_START 0x08169#define KW_I2C_IRQ_MASK 0x0F170171/* State machine states */172enum {173state_idle,174state_addr,175state_read,176state_write,177state_stop,178state_dead179};180181#define WRONG_STATE(name) do {\182printk(KERN_DEBUG "KW: wrong state. Got %s, state: %s " \183"(isr: %02x)\n", \184name, __kw_state_names[host->state], isr); \185} while(0)186187static const char *__kw_state_names[] = {188"state_idle",189"state_addr",190"state_read",191"state_write",192"state_stop",193"state_dead"194};195196static inline u8 __kw_read_reg(struct pmac_i2c_host_kw *host, reg_t reg)197{198return readb(host->base + (((unsigned int)reg) << host->bsteps));199}200201static inline void __kw_write_reg(struct pmac_i2c_host_kw *host,202reg_t reg, u8 val)203{204writeb(val, host->base + (((unsigned)reg) << host->bsteps));205(void)__kw_read_reg(host, reg_subaddr);206}207208#define kw_write_reg(reg, val) __kw_write_reg(host, reg, val)209#define kw_read_reg(reg) __kw_read_reg(host, reg)210211static u8 kw_i2c_wait_interrupt(struct pmac_i2c_host_kw *host)212{213int i, j;214u8 isr;215216for (i = 0; i < 1000; i++) {217isr = kw_read_reg(reg_isr) & KW_I2C_IRQ_MASK;218if (isr != 0)219return isr;220221/* This code is used with the timebase frozen, we cannot rely222* on udelay nor schedule when in polled mode !223* For now, just use a bogus loop....224*/225if (host->polled) {226for (j = 1; j < 100000; j++)227mb();228} else229msleep(1);230}231return isr;232}233234static void kw_i2c_do_stop(struct pmac_i2c_host_kw *host, int result)235{236kw_write_reg(reg_control, KW_I2C_CTL_STOP);237host->state = state_stop;238host->result = result;239}240241242static void kw_i2c_handle_interrupt(struct pmac_i2c_host_kw *host, u8 isr)243{244u8 ack;245246DBG_LOW("kw_handle_interrupt(%s, isr: %x)\n",247__kw_state_names[host->state], isr);248249if (host->state == state_idle) {250printk(KERN_WARNING "low_i2c: Keywest got an out of state"251" interrupt, ignoring\n");252kw_write_reg(reg_isr, isr);253return;254}255256if (isr == 0) {257printk(KERN_WARNING "low_i2c: Timeout in i2c transfer"258" on keywest !\n");259if (host->state != state_stop) {260kw_i2c_do_stop(host, -EIO);261return;262}263ack = kw_read_reg(reg_status);264if (ack & KW_I2C_STAT_BUSY)265kw_write_reg(reg_status, 0);266host->state = state_idle;267kw_write_reg(reg_ier, 0x00);268if (!host->polled)269complete(&host->complete);270return;271}272273if (isr & KW_I2C_IRQ_ADDR) {274ack = kw_read_reg(reg_status);275if (host->state != state_addr) {276WRONG_STATE("KW_I2C_IRQ_ADDR");277kw_i2c_do_stop(host, -EIO);278}279if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {280host->result = -ENXIO;281host->state = state_stop;282DBG_LOW("KW: NAK on address\n");283} else {284if (host->len == 0)285kw_i2c_do_stop(host, 0);286else if (host->rw) {287host->state = state_read;288if (host->len > 1)289kw_write_reg(reg_control,290KW_I2C_CTL_AAK);291} else {292host->state = state_write;293kw_write_reg(reg_data, *(host->data++));294host->len--;295}296}297kw_write_reg(reg_isr, KW_I2C_IRQ_ADDR);298}299300if (isr & KW_I2C_IRQ_DATA) {301if (host->state == state_read) {302*(host->data++) = kw_read_reg(reg_data);303host->len--;304kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);305if (host->len == 0)306host->state = state_stop;307else if (host->len == 1)308kw_write_reg(reg_control, 0);309} else if (host->state == state_write) {310ack = kw_read_reg(reg_status);311if ((ack & KW_I2C_STAT_LAST_AAK) == 0) {312DBG_LOW("KW: nack on data write\n");313host->result = -EFBIG;314host->state = state_stop;315} else if (host->len) {316kw_write_reg(reg_data, *(host->data++));317host->len--;318} else319kw_i2c_do_stop(host, 0);320} else {321WRONG_STATE("KW_I2C_IRQ_DATA");322if (host->state != state_stop)323kw_i2c_do_stop(host, -EIO);324}325kw_write_reg(reg_isr, KW_I2C_IRQ_DATA);326}327328if (isr & KW_I2C_IRQ_STOP) {329kw_write_reg(reg_isr, KW_I2C_IRQ_STOP);330if (host->state != state_stop) {331WRONG_STATE("KW_I2C_IRQ_STOP");332host->result = -EIO;333}334host->state = state_idle;335if (!host->polled)336complete(&host->complete);337}338339/* Below should only happen in manual mode which we don't use ... */340if (isr & KW_I2C_IRQ_START)341kw_write_reg(reg_isr, KW_I2C_IRQ_START);342343}344345/* Interrupt handler */346static irqreturn_t kw_i2c_irq(int irq, void *dev_id)347{348struct pmac_i2c_host_kw *host = dev_id;349unsigned long flags;350351spin_lock_irqsave(&host->lock, flags);352del_timer(&host->timeout_timer);353kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));354if (host->state != state_idle) {355host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;356add_timer(&host->timeout_timer);357}358spin_unlock_irqrestore(&host->lock, flags);359return IRQ_HANDLED;360}361362static void kw_i2c_timeout(unsigned long data)363{364struct pmac_i2c_host_kw *host = (struct pmac_i2c_host_kw *)data;365unsigned long flags;366367spin_lock_irqsave(&host->lock, flags);368kw_i2c_handle_interrupt(host, kw_read_reg(reg_isr));369if (host->state != state_idle) {370host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;371add_timer(&host->timeout_timer);372}373spin_unlock_irqrestore(&host->lock, flags);374}375376static int kw_i2c_open(struct pmac_i2c_bus *bus)377{378struct pmac_i2c_host_kw *host = bus->hostdata;379mutex_lock(&host->mutex);380return 0;381}382383static void kw_i2c_close(struct pmac_i2c_bus *bus)384{385struct pmac_i2c_host_kw *host = bus->hostdata;386mutex_unlock(&host->mutex);387}388389static int kw_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,390u32 subaddr, u8 *data, int len)391{392struct pmac_i2c_host_kw *host = bus->hostdata;393u8 mode_reg = host->speed;394int use_irq = host->irq != NO_IRQ && !bus->polled;395396/* Setup mode & subaddress if any */397switch(bus->mode) {398case pmac_i2c_mode_dumb:399return -EINVAL;400case pmac_i2c_mode_std:401mode_reg |= KW_I2C_MODE_STANDARD;402if (subsize != 0)403return -EINVAL;404break;405case pmac_i2c_mode_stdsub:406mode_reg |= KW_I2C_MODE_STANDARDSUB;407if (subsize != 1)408return -EINVAL;409break;410case pmac_i2c_mode_combined:411mode_reg |= KW_I2C_MODE_COMBINED;412if (subsize != 1)413return -EINVAL;414break;415}416417/* Setup channel & clear pending irqs */418kw_write_reg(reg_isr, kw_read_reg(reg_isr));419kw_write_reg(reg_mode, mode_reg | (bus->channel << 4));420kw_write_reg(reg_status, 0);421422/* Set up address and r/w bit, strip possible stale bus number from423* address top bits424*/425kw_write_reg(reg_addr, addrdir & 0xff);426427/* Set up the sub address */428if ((mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_STANDARDSUB429|| (mode_reg & KW_I2C_MODE_MODE_MASK) == KW_I2C_MODE_COMBINED)430kw_write_reg(reg_subaddr, subaddr);431432/* Prepare for async operations */433host->data = data;434host->len = len;435host->state = state_addr;436host->result = 0;437host->rw = (addrdir & 1);438host->polled = bus->polled;439440/* Enable interrupt if not using polled mode and interrupt is441* available442*/443if (use_irq) {444/* Clear completion */445INIT_COMPLETION(host->complete);446/* Ack stale interrupts */447kw_write_reg(reg_isr, kw_read_reg(reg_isr));448/* Arm timeout */449host->timeout_timer.expires = jiffies + KW_POLL_TIMEOUT;450add_timer(&host->timeout_timer);451/* Enable emission */452kw_write_reg(reg_ier, KW_I2C_IRQ_MASK);453}454455/* Start sending address */456kw_write_reg(reg_control, KW_I2C_CTL_XADDR);457458/* Wait for completion */459if (use_irq)460wait_for_completion(&host->complete);461else {462while(host->state != state_idle) {463unsigned long flags;464465u8 isr = kw_i2c_wait_interrupt(host);466spin_lock_irqsave(&host->lock, flags);467kw_i2c_handle_interrupt(host, isr);468spin_unlock_irqrestore(&host->lock, flags);469}470}471472/* Disable emission */473kw_write_reg(reg_ier, 0);474475return host->result;476}477478static struct pmac_i2c_host_kw *__init kw_i2c_host_init(struct device_node *np)479{480struct pmac_i2c_host_kw *host;481const u32 *psteps, *prate, *addrp;482u32 steps;483484host = kzalloc(sizeof(struct pmac_i2c_host_kw), GFP_KERNEL);485if (host == NULL) {486printk(KERN_ERR "low_i2c: Can't allocate host for %s\n",487np->full_name);488return NULL;489}490491/* Apple is kind enough to provide a valid AAPL,address property492* on all i2c keywest nodes so far ... we would have to fallback493* to macio parsing if that wasn't the case494*/495addrp = of_get_property(np, "AAPL,address", NULL);496if (addrp == NULL) {497printk(KERN_ERR "low_i2c: Can't find address for %s\n",498np->full_name);499kfree(host);500return NULL;501}502mutex_init(&host->mutex);503init_completion(&host->complete);504spin_lock_init(&host->lock);505init_timer(&host->timeout_timer);506host->timeout_timer.function = kw_i2c_timeout;507host->timeout_timer.data = (unsigned long)host;508509psteps = of_get_property(np, "AAPL,address-step", NULL);510steps = psteps ? (*psteps) : 0x10;511for (host->bsteps = 0; (steps & 0x01) == 0; host->bsteps++)512steps >>= 1;513/* Select interface rate */514host->speed = KW_I2C_MODE_25KHZ;515prate = of_get_property(np, "AAPL,i2c-rate", NULL);516if (prate) switch(*prate) {517case 100:518host->speed = KW_I2C_MODE_100KHZ;519break;520case 50:521host->speed = KW_I2C_MODE_50KHZ;522break;523case 25:524host->speed = KW_I2C_MODE_25KHZ;525break;526}527host->irq = irq_of_parse_and_map(np, 0);528if (host->irq == NO_IRQ)529printk(KERN_WARNING530"low_i2c: Failed to map interrupt for %s\n",531np->full_name);532533host->base = ioremap((*addrp), 0x1000);534if (host->base == NULL) {535printk(KERN_ERR "low_i2c: Can't map registers for %s\n",536np->full_name);537kfree(host);538return NULL;539}540541/* Make sure IRQ is disabled */542kw_write_reg(reg_ier, 0);543544/* Request chip interrupt. We set IRQF_NO_SUSPEND because we don't545* want that interrupt disabled between the 2 passes of driver546* suspend or we'll have issues running the pfuncs547*/548if (request_irq(host->irq, kw_i2c_irq, IRQF_NO_SUSPEND,549"keywest i2c", host))550host->irq = NO_IRQ;551552printk(KERN_INFO "KeyWest i2c @0x%08x irq %d %s\n",553*addrp, host->irq, np->full_name);554555return host;556}557558559static void __init kw_i2c_add(struct pmac_i2c_host_kw *host,560struct device_node *controller,561struct device_node *busnode,562int channel)563{564struct pmac_i2c_bus *bus;565566bus = kzalloc(sizeof(struct pmac_i2c_bus), GFP_KERNEL);567if (bus == NULL)568return;569570bus->controller = of_node_get(controller);571bus->busnode = of_node_get(busnode);572bus->type = pmac_i2c_bus_keywest;573bus->hostdata = host;574bus->channel = channel;575bus->mode = pmac_i2c_mode_std;576bus->open = kw_i2c_open;577bus->close = kw_i2c_close;578bus->xfer = kw_i2c_xfer;579mutex_init(&bus->mutex);580if (controller == busnode)581bus->flags = pmac_i2c_multibus;582list_add(&bus->link, &pmac_i2c_busses);583584printk(KERN_INFO " channel %d bus %s\n", channel,585(controller == busnode) ? "<multibus>" : busnode->full_name);586}587588static void __init kw_i2c_probe(void)589{590struct device_node *np, *child, *parent;591592/* Probe keywest-i2c busses */593for_each_compatible_node(np, "i2c","keywest-i2c") {594struct pmac_i2c_host_kw *host;595int multibus;596597/* Found one, init a host structure */598host = kw_i2c_host_init(np);599if (host == NULL)600continue;601602/* Now check if we have a multibus setup (old style) or if we603* have proper bus nodes. Note that the "new" way (proper bus604* nodes) might cause us to not create some busses that are605* kept hidden in the device-tree. In the future, we might606* want to work around that by creating busses without a node607* but not for now608*/609child = of_get_next_child(np, NULL);610multibus = !child || strcmp(child->name, "i2c-bus");611of_node_put(child);612613/* For a multibus setup, we get the bus count based on the614* parent type615*/616if (multibus) {617int chans, i;618619parent = of_get_parent(np);620if (parent == NULL)621continue;622chans = parent->name[0] == 'u' ? 2 : 1;623for (i = 0; i < chans; i++)624kw_i2c_add(host, np, np, i);625} else {626for (child = NULL;627(child = of_get_next_child(np, child)) != NULL;) {628const u32 *reg = of_get_property(child,629"reg", NULL);630if (reg == NULL)631continue;632kw_i2c_add(host, np, child, *reg);633}634}635}636}637638639/*640*641* PMU implementation642*643*/644645#ifdef CONFIG_ADB_PMU646647/*648* i2c command block to the PMU649*/650struct pmu_i2c_hdr {651u8 bus;652u8 mode;653u8 bus2;654u8 address;655u8 sub_addr;656u8 comb_addr;657u8 count;658u8 data[];659};660661static void pmu_i2c_complete(struct adb_request *req)662{663complete(req->arg);664}665666static int pmu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,667u32 subaddr, u8 *data, int len)668{669struct adb_request *req = bus->hostdata;670struct pmu_i2c_hdr *hdr = (struct pmu_i2c_hdr *)&req->data[1];671struct completion comp;672int read = addrdir & 1;673int retry;674int rc = 0;675676/* For now, limit ourselves to 16 bytes transfers */677if (len > 16)678return -EINVAL;679680init_completion(&comp);681682for (retry = 0; retry < 16; retry++) {683memset(req, 0, sizeof(struct adb_request));684hdr->bus = bus->channel;685hdr->count = len;686687switch(bus->mode) {688case pmac_i2c_mode_std:689if (subsize != 0)690return -EINVAL;691hdr->address = addrdir;692hdr->mode = PMU_I2C_MODE_SIMPLE;693break;694case pmac_i2c_mode_stdsub:695case pmac_i2c_mode_combined:696if (subsize != 1)697return -EINVAL;698hdr->address = addrdir & 0xfe;699hdr->comb_addr = addrdir;700hdr->sub_addr = subaddr;701if (bus->mode == pmac_i2c_mode_stdsub)702hdr->mode = PMU_I2C_MODE_STDSUB;703else704hdr->mode = PMU_I2C_MODE_COMBINED;705break;706default:707return -EINVAL;708}709710INIT_COMPLETION(comp);711req->data[0] = PMU_I2C_CMD;712req->reply[0] = 0xff;713req->nbytes = sizeof(struct pmu_i2c_hdr) + 1;714req->done = pmu_i2c_complete;715req->arg = ∁716if (!read && len) {717memcpy(hdr->data, data, len);718req->nbytes += len;719}720rc = pmu_queue_request(req);721if (rc)722return rc;723wait_for_completion(&comp);724if (req->reply[0] == PMU_I2C_STATUS_OK)725break;726msleep(15);727}728if (req->reply[0] != PMU_I2C_STATUS_OK)729return -EIO;730731for (retry = 0; retry < 16; retry++) {732memset(req, 0, sizeof(struct adb_request));733734/* I know that looks like a lot, slow as hell, but darwin735* does it so let's be on the safe side for now736*/737msleep(15);738739hdr->bus = PMU_I2C_BUS_STATUS;740741INIT_COMPLETION(comp);742req->data[0] = PMU_I2C_CMD;743req->reply[0] = 0xff;744req->nbytes = 2;745req->done = pmu_i2c_complete;746req->arg = ∁747rc = pmu_queue_request(req);748if (rc)749return rc;750wait_for_completion(&comp);751752if (req->reply[0] == PMU_I2C_STATUS_OK && !read)753return 0;754if (req->reply[0] == PMU_I2C_STATUS_DATAREAD && read) {755int rlen = req->reply_len - 1;756757if (rlen != len) {758printk(KERN_WARNING "low_i2c: PMU returned %d"759" bytes, expected %d !\n", rlen, len);760return -EIO;761}762if (len)763memcpy(data, &req->reply[1], len);764return 0;765}766}767return -EIO;768}769770static void __init pmu_i2c_probe(void)771{772struct pmac_i2c_bus *bus;773struct device_node *busnode;774int channel, sz;775776if (!pmu_present())777return;778779/* There might or might not be a "pmu-i2c" node, we use that780* or via-pmu itself, whatever we find. I haven't seen a machine781* with separate bus nodes, so we assume a multibus setup782*/783busnode = of_find_node_by_name(NULL, "pmu-i2c");784if (busnode == NULL)785busnode = of_find_node_by_name(NULL, "via-pmu");786if (busnode == NULL)787return;788789printk(KERN_INFO "PMU i2c %s\n", busnode->full_name);790791/*792* We add bus 1 and 2 only for now, bus 0 is "special"793*/794for (channel = 1; channel <= 2; channel++) {795sz = sizeof(struct pmac_i2c_bus) + sizeof(struct adb_request);796bus = kzalloc(sz, GFP_KERNEL);797if (bus == NULL)798return;799800bus->controller = busnode;801bus->busnode = busnode;802bus->type = pmac_i2c_bus_pmu;803bus->channel = channel;804bus->mode = pmac_i2c_mode_std;805bus->hostdata = bus + 1;806bus->xfer = pmu_i2c_xfer;807mutex_init(&bus->mutex);808bus->flags = pmac_i2c_multibus;809list_add(&bus->link, &pmac_i2c_busses);810811printk(KERN_INFO " channel %d bus <multibus>\n", channel);812}813}814815#endif /* CONFIG_ADB_PMU */816817818/*819*820* SMU implementation821*822*/823824#ifdef CONFIG_PMAC_SMU825826static void smu_i2c_complete(struct smu_i2c_cmd *cmd, void *misc)827{828complete(misc);829}830831static int smu_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,832u32 subaddr, u8 *data, int len)833{834struct smu_i2c_cmd *cmd = bus->hostdata;835struct completion comp;836int read = addrdir & 1;837int rc = 0;838839if ((read && len > SMU_I2C_READ_MAX) ||840((!read) && len > SMU_I2C_WRITE_MAX))841return -EINVAL;842843memset(cmd, 0, sizeof(struct smu_i2c_cmd));844cmd->info.bus = bus->channel;845cmd->info.devaddr = addrdir;846cmd->info.datalen = len;847848switch(bus->mode) {849case pmac_i2c_mode_std:850if (subsize != 0)851return -EINVAL;852cmd->info.type = SMU_I2C_TRANSFER_SIMPLE;853break;854case pmac_i2c_mode_stdsub:855case pmac_i2c_mode_combined:856if (subsize > 3 || subsize < 1)857return -EINVAL;858cmd->info.sublen = subsize;859/* that's big-endian only but heh ! */860memcpy(&cmd->info.subaddr, ((char *)&subaddr) + (4 - subsize),861subsize);862if (bus->mode == pmac_i2c_mode_stdsub)863cmd->info.type = SMU_I2C_TRANSFER_STDSUB;864else865cmd->info.type = SMU_I2C_TRANSFER_COMBINED;866break;867default:868return -EINVAL;869}870if (!read && len)871memcpy(cmd->info.data, data, len);872873init_completion(&comp);874cmd->done = smu_i2c_complete;875cmd->misc = ∁876rc = smu_queue_i2c(cmd);877if (rc < 0)878return rc;879wait_for_completion(&comp);880rc = cmd->status;881882if (read && len)883memcpy(data, cmd->info.data, len);884return rc < 0 ? rc : 0;885}886887static void __init smu_i2c_probe(void)888{889struct device_node *controller, *busnode;890struct pmac_i2c_bus *bus;891const u32 *reg;892int sz;893894if (!smu_present())895return;896897controller = of_find_node_by_name(NULL, "smu-i2c-control");898if (controller == NULL)899controller = of_find_node_by_name(NULL, "smu");900if (controller == NULL)901return;902903printk(KERN_INFO "SMU i2c %s\n", controller->full_name);904905/* Look for childs, note that they might not be of the right906* type as older device trees mix i2c busses and other things907* at the same level908*/909for (busnode = NULL;910(busnode = of_get_next_child(controller, busnode)) != NULL;) {911if (strcmp(busnode->type, "i2c") &&912strcmp(busnode->type, "i2c-bus"))913continue;914reg = of_get_property(busnode, "reg", NULL);915if (reg == NULL)916continue;917918sz = sizeof(struct pmac_i2c_bus) + sizeof(struct smu_i2c_cmd);919bus = kzalloc(sz, GFP_KERNEL);920if (bus == NULL)921return;922923bus->controller = controller;924bus->busnode = of_node_get(busnode);925bus->type = pmac_i2c_bus_smu;926bus->channel = *reg;927bus->mode = pmac_i2c_mode_std;928bus->hostdata = bus + 1;929bus->xfer = smu_i2c_xfer;930mutex_init(&bus->mutex);931bus->flags = 0;932list_add(&bus->link, &pmac_i2c_busses);933934printk(KERN_INFO " channel %x bus %s\n",935bus->channel, busnode->full_name);936}937}938939#endif /* CONFIG_PMAC_SMU */940941/*942*943* Core code944*945*/946947948struct pmac_i2c_bus *pmac_i2c_find_bus(struct device_node *node)949{950struct device_node *p = of_node_get(node);951struct device_node *prev = NULL;952struct pmac_i2c_bus *bus;953954while(p) {955list_for_each_entry(bus, &pmac_i2c_busses, link) {956if (p == bus->busnode) {957if (prev && bus->flags & pmac_i2c_multibus) {958const u32 *reg;959reg = of_get_property(prev, "reg",960NULL);961if (!reg)962continue;963if (((*reg) >> 8) != bus->channel)964continue;965}966of_node_put(p);967of_node_put(prev);968return bus;969}970}971of_node_put(prev);972prev = p;973p = of_get_parent(p);974}975return NULL;976}977EXPORT_SYMBOL_GPL(pmac_i2c_find_bus);978979u8 pmac_i2c_get_dev_addr(struct device_node *device)980{981const u32 *reg = of_get_property(device, "reg", NULL);982983if (reg == NULL)984return 0;985986return (*reg) & 0xff;987}988EXPORT_SYMBOL_GPL(pmac_i2c_get_dev_addr);989990struct device_node *pmac_i2c_get_controller(struct pmac_i2c_bus *bus)991{992return bus->controller;993}994EXPORT_SYMBOL_GPL(pmac_i2c_get_controller);995996struct device_node *pmac_i2c_get_bus_node(struct pmac_i2c_bus *bus)997{998return bus->busnode;999}1000EXPORT_SYMBOL_GPL(pmac_i2c_get_bus_node);10011002int pmac_i2c_get_type(struct pmac_i2c_bus *bus)1003{1004return bus->type;1005}1006EXPORT_SYMBOL_GPL(pmac_i2c_get_type);10071008int pmac_i2c_get_flags(struct pmac_i2c_bus *bus)1009{1010return bus->flags;1011}1012EXPORT_SYMBOL_GPL(pmac_i2c_get_flags);10131014int pmac_i2c_get_channel(struct pmac_i2c_bus *bus)1015{1016return bus->channel;1017}1018EXPORT_SYMBOL_GPL(pmac_i2c_get_channel);101910201021struct i2c_adapter *pmac_i2c_get_adapter(struct pmac_i2c_bus *bus)1022{1023return &bus->adapter;1024}1025EXPORT_SYMBOL_GPL(pmac_i2c_get_adapter);10261027struct pmac_i2c_bus *pmac_i2c_adapter_to_bus(struct i2c_adapter *adapter)1028{1029struct pmac_i2c_bus *bus;10301031list_for_each_entry(bus, &pmac_i2c_busses, link)1032if (&bus->adapter == adapter)1033return bus;1034return NULL;1035}1036EXPORT_SYMBOL_GPL(pmac_i2c_adapter_to_bus);10371038int pmac_i2c_match_adapter(struct device_node *dev, struct i2c_adapter *adapter)1039{1040struct pmac_i2c_bus *bus = pmac_i2c_find_bus(dev);10411042if (bus == NULL)1043return 0;1044return (&bus->adapter == adapter);1045}1046EXPORT_SYMBOL_GPL(pmac_i2c_match_adapter);10471048int pmac_low_i2c_lock(struct device_node *np)1049{1050struct pmac_i2c_bus *bus, *found = NULL;10511052list_for_each_entry(bus, &pmac_i2c_busses, link) {1053if (np == bus->controller) {1054found = bus;1055break;1056}1057}1058if (!found)1059return -ENODEV;1060return pmac_i2c_open(bus, 0);1061}1062EXPORT_SYMBOL_GPL(pmac_low_i2c_lock);10631064int pmac_low_i2c_unlock(struct device_node *np)1065{1066struct pmac_i2c_bus *bus, *found = NULL;10671068list_for_each_entry(bus, &pmac_i2c_busses, link) {1069if (np == bus->controller) {1070found = bus;1071break;1072}1073}1074if (!found)1075return -ENODEV;1076pmac_i2c_close(bus);1077return 0;1078}1079EXPORT_SYMBOL_GPL(pmac_low_i2c_unlock);108010811082int pmac_i2c_open(struct pmac_i2c_bus *bus, int polled)1083{1084int rc;10851086mutex_lock(&bus->mutex);1087bus->polled = polled || pmac_i2c_force_poll;1088bus->opened = 1;1089bus->mode = pmac_i2c_mode_std;1090if (bus->open && (rc = bus->open(bus)) != 0) {1091bus->opened = 0;1092mutex_unlock(&bus->mutex);1093return rc;1094}1095return 0;1096}1097EXPORT_SYMBOL_GPL(pmac_i2c_open);10981099void pmac_i2c_close(struct pmac_i2c_bus *bus)1100{1101WARN_ON(!bus->opened);1102if (bus->close)1103bus->close(bus);1104bus->opened = 0;1105mutex_unlock(&bus->mutex);1106}1107EXPORT_SYMBOL_GPL(pmac_i2c_close);11081109int pmac_i2c_setmode(struct pmac_i2c_bus *bus, int mode)1110{1111WARN_ON(!bus->opened);11121113/* Report me if you see the error below as there might be a new1114* "combined4" mode that I need to implement for the SMU bus1115*/1116if (mode < pmac_i2c_mode_dumb || mode > pmac_i2c_mode_combined) {1117printk(KERN_ERR "low_i2c: Invalid mode %d requested on"1118" bus %s !\n", mode, bus->busnode->full_name);1119return -EINVAL;1120}1121bus->mode = mode;11221123return 0;1124}1125EXPORT_SYMBOL_GPL(pmac_i2c_setmode);11261127int pmac_i2c_xfer(struct pmac_i2c_bus *bus, u8 addrdir, int subsize,1128u32 subaddr, u8 *data, int len)1129{1130int rc;11311132WARN_ON(!bus->opened);11331134DBG("xfer() chan=%d, addrdir=0x%x, mode=%d, subsize=%d, subaddr=0x%x,"1135" %d bytes, bus %s\n", bus->channel, addrdir, bus->mode, subsize,1136subaddr, len, bus->busnode->full_name);11371138rc = bus->xfer(bus, addrdir, subsize, subaddr, data, len);11391140#ifdef DEBUG1141if (rc)1142DBG("xfer error %d\n", rc);1143#endif1144return rc;1145}1146EXPORT_SYMBOL_GPL(pmac_i2c_xfer);11471148/* some quirks for platform function decoding */1149enum {1150pmac_i2c_quirk_invmask = 0x00000001u,1151pmac_i2c_quirk_skip = 0x00000002u,1152};11531154static void pmac_i2c_devscan(void (*callback)(struct device_node *dev,1155int quirks))1156{1157struct pmac_i2c_bus *bus;1158struct device_node *np;1159static struct whitelist_ent {1160char *name;1161char *compatible;1162int quirks;1163} whitelist[] = {1164/* XXX Study device-tree's & apple drivers are get the quirks1165* right !1166*/1167/* Workaround: It seems that running the clockspreading1168* properties on the eMac will cause lockups during boot.1169* The machine seems to work fine without that. So for now,1170* let's make sure i2c-hwclock doesn't match about "imic"1171* clocks and we'll figure out if we really need to do1172* something special about those later.1173*/1174{ "i2c-hwclock", "imic5002", pmac_i2c_quirk_skip },1175{ "i2c-hwclock", "imic5003", pmac_i2c_quirk_skip },1176{ "i2c-hwclock", NULL, pmac_i2c_quirk_invmask },1177{ "i2c-cpu-voltage", NULL, 0},1178{ "temp-monitor", NULL, 0 },1179{ "supply-monitor", NULL, 0 },1180{ NULL, NULL, 0 },1181};11821183/* Only some devices need to have platform functions instanciated1184* here. For now, we have a table. Others, like 9554 i2c GPIOs used1185* on Xserve, if we ever do a driver for them, will use their own1186* platform function instance1187*/1188list_for_each_entry(bus, &pmac_i2c_busses, link) {1189for (np = NULL;1190(np = of_get_next_child(bus->busnode, np)) != NULL;) {1191struct whitelist_ent *p;1192/* If multibus, check if device is on that bus */1193if (bus->flags & pmac_i2c_multibus)1194if (bus != pmac_i2c_find_bus(np))1195continue;1196for (p = whitelist; p->name != NULL; p++) {1197if (strcmp(np->name, p->name))1198continue;1199if (p->compatible &&1200!of_device_is_compatible(np, p->compatible))1201continue;1202if (p->quirks & pmac_i2c_quirk_skip)1203break;1204callback(np, p->quirks);1205break;1206}1207}1208}1209}12101211#define MAX_I2C_DATA 6412121213struct pmac_i2c_pf_inst1214{1215struct pmac_i2c_bus *bus;1216u8 addr;1217u8 buffer[MAX_I2C_DATA];1218u8 scratch[MAX_I2C_DATA];1219int bytes;1220int quirks;1221};12221223static void* pmac_i2c_do_begin(struct pmf_function *func, struct pmf_args *args)1224{1225struct pmac_i2c_pf_inst *inst;1226struct pmac_i2c_bus *bus;12271228bus = pmac_i2c_find_bus(func->node);1229if (bus == NULL) {1230printk(KERN_ERR "low_i2c: Can't find bus for %s (pfunc)\n",1231func->node->full_name);1232return NULL;1233}1234if (pmac_i2c_open(bus, 0)) {1235printk(KERN_ERR "low_i2c: Can't open i2c bus for %s (pfunc)\n",1236func->node->full_name);1237return NULL;1238}12391240/* XXX might need GFP_ATOMIC when called during the suspend process,1241* but then, there are already lots of issues with suspending when1242* near OOM that need to be resolved, the allocator itself should1243* probably make GFP_NOIO implicit during suspend1244*/1245inst = kzalloc(sizeof(struct pmac_i2c_pf_inst), GFP_KERNEL);1246if (inst == NULL) {1247pmac_i2c_close(bus);1248return NULL;1249}1250inst->bus = bus;1251inst->addr = pmac_i2c_get_dev_addr(func->node);1252inst->quirks = (int)(long)func->driver_data;1253return inst;1254}12551256static void pmac_i2c_do_end(struct pmf_function *func, void *instdata)1257{1258struct pmac_i2c_pf_inst *inst = instdata;12591260if (inst == NULL)1261return;1262pmac_i2c_close(inst->bus);1263kfree(inst);1264}12651266static int pmac_i2c_do_read(PMF_STD_ARGS, u32 len)1267{1268struct pmac_i2c_pf_inst *inst = instdata;12691270inst->bytes = len;1271return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 0, 0,1272inst->buffer, len);1273}12741275static int pmac_i2c_do_write(PMF_STD_ARGS, u32 len, const u8 *data)1276{1277struct pmac_i2c_pf_inst *inst = instdata;12781279return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,1280(u8 *)data, len);1281}12821283/* This function is used to do the masking & OR'ing for the "rmw" type1284* callbacks. Ze should apply the mask and OR in the values in the1285* buffer before writing back. The problem is that it seems that1286* various darwin drivers implement the mask/or differently, thus1287* we need to check the quirks first1288*/1289static void pmac_i2c_do_apply_rmw(struct pmac_i2c_pf_inst *inst,1290u32 len, const u8 *mask, const u8 *val)1291{1292int i;12931294if (inst->quirks & pmac_i2c_quirk_invmask) {1295for (i = 0; i < len; i ++)1296inst->scratch[i] = (inst->buffer[i] & mask[i]) | val[i];1297} else {1298for (i = 0; i < len; i ++)1299inst->scratch[i] = (inst->buffer[i] & ~mask[i])1300| (val[i] & mask[i]);1301}1302}13031304static int pmac_i2c_do_rmw(PMF_STD_ARGS, u32 masklen, u32 valuelen,1305u32 totallen, const u8 *maskdata,1306const u8 *valuedata)1307{1308struct pmac_i2c_pf_inst *inst = instdata;13091310if (masklen > inst->bytes || valuelen > inst->bytes ||1311totallen > inst->bytes || valuelen > masklen)1312return -EINVAL;13131314pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);13151316return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 0, 0,1317inst->scratch, totallen);1318}13191320static int pmac_i2c_do_read_sub(PMF_STD_ARGS, u8 subaddr, u32 len)1321{1322struct pmac_i2c_pf_inst *inst = instdata;13231324inst->bytes = len;1325return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_read, 1, subaddr,1326inst->buffer, len);1327}13281329static int pmac_i2c_do_write_sub(PMF_STD_ARGS, u8 subaddr, u32 len,1330const u8 *data)1331{1332struct pmac_i2c_pf_inst *inst = instdata;13331334return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,1335subaddr, (u8 *)data, len);1336}13371338static int pmac_i2c_do_set_mode(PMF_STD_ARGS, int mode)1339{1340struct pmac_i2c_pf_inst *inst = instdata;13411342return pmac_i2c_setmode(inst->bus, mode);1343}13441345static int pmac_i2c_do_rmw_sub(PMF_STD_ARGS, u8 subaddr, u32 masklen,1346u32 valuelen, u32 totallen, const u8 *maskdata,1347const u8 *valuedata)1348{1349struct pmac_i2c_pf_inst *inst = instdata;13501351if (masklen > inst->bytes || valuelen > inst->bytes ||1352totallen > inst->bytes || valuelen > masklen)1353return -EINVAL;13541355pmac_i2c_do_apply_rmw(inst, masklen, maskdata, valuedata);13561357return pmac_i2c_xfer(inst->bus, inst->addr | pmac_i2c_write, 1,1358subaddr, inst->scratch, totallen);1359}13601361static int pmac_i2c_do_mask_and_comp(PMF_STD_ARGS, u32 len,1362const u8 *maskdata,1363const u8 *valuedata)1364{1365struct pmac_i2c_pf_inst *inst = instdata;1366int i, match;13671368/* Get return value pointer, it's assumed to be a u32 */1369if (!args || !args->count || !args->u[0].p)1370return -EINVAL;13711372/* Check buffer */1373if (len > inst->bytes)1374return -EINVAL;13751376for (i = 0, match = 1; match && i < len; i ++)1377if ((inst->buffer[i] & maskdata[i]) != valuedata[i])1378match = 0;1379*args->u[0].p = match;1380return 0;1381}13821383static int pmac_i2c_do_delay(PMF_STD_ARGS, u32 duration)1384{1385msleep((duration + 999) / 1000);1386return 0;1387}138813891390static struct pmf_handlers pmac_i2c_pfunc_handlers = {1391.begin = pmac_i2c_do_begin,1392.end = pmac_i2c_do_end,1393.read_i2c = pmac_i2c_do_read,1394.write_i2c = pmac_i2c_do_write,1395.rmw_i2c = pmac_i2c_do_rmw,1396.read_i2c_sub = pmac_i2c_do_read_sub,1397.write_i2c_sub = pmac_i2c_do_write_sub,1398.rmw_i2c_sub = pmac_i2c_do_rmw_sub,1399.set_i2c_mode = pmac_i2c_do_set_mode,1400.mask_and_compare = pmac_i2c_do_mask_and_comp,1401.delay = pmac_i2c_do_delay,1402};14031404static void __init pmac_i2c_dev_create(struct device_node *np, int quirks)1405{1406DBG("dev_create(%s)\n", np->full_name);14071408pmf_register_driver(np, &pmac_i2c_pfunc_handlers,1409(void *)(long)quirks);1410}14111412static void __init pmac_i2c_dev_init(struct device_node *np, int quirks)1413{1414DBG("dev_create(%s)\n", np->full_name);14151416pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_INIT, NULL);1417}14181419static void pmac_i2c_dev_suspend(struct device_node *np, int quirks)1420{1421DBG("dev_suspend(%s)\n", np->full_name);1422pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_SLEEP, NULL);1423}14241425static void pmac_i2c_dev_resume(struct device_node *np, int quirks)1426{1427DBG("dev_resume(%s)\n", np->full_name);1428pmf_do_functions(np, NULL, 0, PMF_FLAGS_ON_WAKE, NULL);1429}14301431void pmac_pfunc_i2c_suspend(void)1432{1433pmac_i2c_devscan(pmac_i2c_dev_suspend);1434}14351436void pmac_pfunc_i2c_resume(void)1437{1438pmac_i2c_devscan(pmac_i2c_dev_resume);1439}14401441/*1442* Initialize us: probe all i2c busses on the machine, instantiate1443* busses and platform functions as needed.1444*/1445/* This is non-static as it might be called early by smp code */1446int __init pmac_i2c_init(void)1447{1448static int i2c_inited;14491450if (i2c_inited)1451return 0;1452i2c_inited = 1;14531454/* Probe keywest-i2c busses */1455kw_i2c_probe();14561457#ifdef CONFIG_ADB_PMU1458/* Probe PMU i2c busses */1459pmu_i2c_probe();1460#endif14611462#ifdef CONFIG_PMAC_SMU1463/* Probe SMU i2c busses */1464smu_i2c_probe();1465#endif14661467/* Now add plaform functions for some known devices */1468pmac_i2c_devscan(pmac_i2c_dev_create);14691470return 0;1471}1472machine_arch_initcall(powermac, pmac_i2c_init);14731474/* Since pmac_i2c_init can be called too early for the platform device1475* registration, we need to do it at a later time. In our case, subsys1476* happens to fit well, though I agree it's a bit of a hack...1477*/1478static int __init pmac_i2c_create_platform_devices(void)1479{1480struct pmac_i2c_bus *bus;1481int i = 0;14821483/* In the case where we are initialized from smp_init(), we must1484* not use the timer (and thus the irq). It's safe from now on1485* though1486*/1487pmac_i2c_force_poll = 0;14881489/* Create platform devices */1490list_for_each_entry(bus, &pmac_i2c_busses, link) {1491bus->platform_dev =1492platform_device_alloc("i2c-powermac", i++);1493if (bus->platform_dev == NULL)1494return -ENOMEM;1495bus->platform_dev->dev.platform_data = bus;1496platform_device_add(bus->platform_dev);1497}14981499/* Now call platform "init" functions */1500pmac_i2c_devscan(pmac_i2c_dev_init);15011502return 0;1503}1504machine_subsys_initcall(powermac, pmac_i2c_create_platform_devices);150515061507