blob: fb7300837feef25a6b2a80de582baf01031bf22a [file] [log] [blame]
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001/*
2 * PS3 virtual uart
3 *
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080023#include <linux/module.h>
24#include <linux/interrupt.h>
Geoff Levandea1547d2007-02-06 14:23:47 -080025#include <linux/workqueue.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070026#include <linux/bitops.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080027#include <asm/ps3.h>
28
Geoff Levand75c86e72007-02-13 17:37:28 -080029#include <asm/firmware.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080030#include <asm/lv1call.h>
Geoff Levand74e95d5d2006-12-08 18:27:47 -080031
32#include "vuart.h"
33
34MODULE_AUTHOR("Sony Corporation");
35MODULE_LICENSE("GPL v2");
Geoff Levand75c86e72007-02-13 17:37:28 -080036MODULE_DESCRIPTION("PS3 vuart");
Geoff Levand74e95d5d2006-12-08 18:27:47 -080037
38/**
39 * vuart - An inter-partition data link service.
40 * port 0: PS3 AV Settings.
41 * port 2: PS3 System Manager.
42 *
43 * The vuart provides a bi-directional byte stream data link between logical
44 * partitions. Its primary role is as a communications link between the guest
45 * OS and the system policy module. The current HV does not support any
46 * connections other than those listed.
47 */
48
49enum {PORT_COUNT = 3,};
50
51enum vuart_param {
52 PARAM_TX_TRIGGER = 0,
53 PARAM_RX_TRIGGER = 1,
54 PARAM_INTERRUPT_MASK = 2,
55 PARAM_RX_BUF_SIZE = 3, /* read only */
56 PARAM_RX_BYTES = 4, /* read only */
57 PARAM_TX_BUF_SIZE = 5, /* read only */
58 PARAM_TX_BYTES = 6, /* read only */
59 PARAM_INTERRUPT_STATUS = 7, /* read only */
60};
61
62enum vuart_interrupt_bit {
63 INTERRUPT_BIT_TX = 0,
64 INTERRUPT_BIT_RX = 1,
65 INTERRUPT_BIT_DISCONNECT = 2,
66};
67
68enum vuart_interrupt_mask {
69 INTERRUPT_MASK_TX = 1,
70 INTERRUPT_MASK_RX = 2,
71 INTERRUPT_MASK_DISCONNECT = 4,
72};
73
74/**
Geoff Levand7626e782007-06-16 08:01:06 +100075 * struct ps3_vuart_port_priv - private vuart device data.
76 */
77
78struct ps3_vuart_port_priv {
79 u64 interrupt_mask;
80
81 struct {
82 spinlock_t lock;
83 struct list_head head;
84 } tx_list;
85 struct {
86 struct ps3_vuart_work work;
87 unsigned long bytes_held;
88 spinlock_t lock;
89 struct list_head head;
90 } rx_list;
91 struct ps3_vuart_stats stats;
92};
93
94static struct ps3_vuart_port_priv *to_port_priv(
95 struct ps3_system_bus_device *dev)
96{
97 BUG_ON(!dev);
98 BUG_ON(!dev->driver_priv);
99 return (struct ps3_vuart_port_priv *)dev->driver_priv;
100}
101
102/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800103 * struct ports_bmp - bitmap indicating ports needing service.
104 *
105 * A 256 bit read only bitmap indicating ports needing service. Do not write
106 * to these bits. Must not cross a page boundary.
107 */
108
109struct ports_bmp {
110 u64 status;
111 u64 unused[3];
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100112} __attribute__((aligned(32)));
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800113
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800114#define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__)
Geoff Levand848cfdc2007-06-16 07:18:14 +1000115static void __maybe_unused _dump_ports_bmp(
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100116 const struct ports_bmp *bmp, const char *func, int line)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800117{
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000118 pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800119}
120
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800121#define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__)
Geoff Levand848cfdc2007-06-16 07:18:14 +1000122static void __maybe_unused _dump_port_params(unsigned int port_number,
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100123 const char *func, int line)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800124{
125#if defined(DEBUG)
126 static const char *strings[] = {
127 "tx_trigger ",
128 "rx_trigger ",
129 "interrupt_mask ",
130 "rx_buf_size ",
131 "rx_bytes ",
132 "tx_buf_size ",
133 "tx_bytes ",
134 "interrupt_status",
135 };
136 int result;
137 unsigned int i;
138 u64 value;
139
140 for (i = 0; i < ARRAY_SIZE(strings); i++) {
141 result = lv1_get_virtual_uart_param(port_number, i, &value);
142
143 if (result) {
144 pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line,
145 port_number, strings[i], ps3_result(result));
146 continue;
147 }
148 pr_debug("%s:%d: port_%u: %s = %lxh\n",
149 func, line, port_number, strings[i], value);
150 }
151#endif
152}
153
154struct vuart_triggers {
155 unsigned long rx;
156 unsigned long tx;
157};
158
Geoff Levand7626e782007-06-16 08:01:06 +1000159int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800160 struct vuart_triggers *trig)
161{
162 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000163 u64 size;
164 u64 val;
165 u64 tx;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800166
Geoff Levand7626e782007-06-16 08:01:06 +1000167 result = lv1_get_virtual_uart_param(dev->port_number,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000168 PARAM_TX_TRIGGER, &tx);
169 trig->tx = tx;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800170
171 if (result) {
172 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
173 __func__, __LINE__, ps3_result(result));
174 return result;
175 }
176
Geoff Levand7626e782007-06-16 08:01:06 +1000177 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800178 PARAM_RX_BUF_SIZE, &size);
179
180 if (result) {
181 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
182 __func__, __LINE__, ps3_result(result));
183 return result;
184 }
185
Geoff Levand7626e782007-06-16 08:01:06 +1000186 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800187 PARAM_RX_TRIGGER, &val);
188
189 if (result) {
190 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
191 __func__, __LINE__, ps3_result(result));
192 return result;
193 }
194
195 trig->rx = size - val;
196
197 dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__,
198 trig->tx, trig->rx);
199
200 return result;
201}
202
Geoff Levand7626e782007-06-16 08:01:06 +1000203int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800204 unsigned int rx)
205{
206 int result;
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000207 u64 size;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800208
Geoff Levand7626e782007-06-16 08:01:06 +1000209 result = lv1_set_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800210 PARAM_TX_TRIGGER, tx);
211
212 if (result) {
213 dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n",
214 __func__, __LINE__, ps3_result(result));
215 return result;
216 }
217
Geoff Levand7626e782007-06-16 08:01:06 +1000218 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800219 PARAM_RX_BUF_SIZE, &size);
220
221 if (result) {
222 dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n",
223 __func__, __LINE__, ps3_result(result));
224 return result;
225 }
226
Geoff Levand7626e782007-06-16 08:01:06 +1000227 result = lv1_set_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800228 PARAM_RX_TRIGGER, size - rx);
229
230 if (result) {
231 dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n",
232 __func__, __LINE__, ps3_result(result));
233 return result;
234 }
235
236 dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__,
237 tx, rx);
238
239 return result;
240}
241
Geoff Levand7626e782007-06-16 08:01:06 +1000242static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800243 u64 *bytes_waiting)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800244{
Geoff Levand7626e782007-06-16 08:01:06 +1000245 int result;
246
247 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800248 PARAM_RX_BYTES, bytes_waiting);
249
250 if (result)
251 dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n",
252 __func__, __LINE__, ps3_result(result));
253
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000254 dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800255 *bytes_waiting);
256 return result;
257}
258
Geoff Levand7626e782007-06-16 08:01:06 +1000259/**
260 * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources.
261 * @dev: The struct ps3_system_bus_device instance.
262 * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables.
263 */
264
265static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800266 unsigned long mask)
267{
268 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000269 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800270
271 dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask);
272
Geoff Levand7626e782007-06-16 08:01:06 +1000273 priv->interrupt_mask = mask;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800274
Geoff Levand7626e782007-06-16 08:01:06 +1000275 result = lv1_set_virtual_uart_param(dev->port_number,
276 PARAM_INTERRUPT_MASK, priv->interrupt_mask);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800277
278 if (result)
279 dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n",
280 __func__, __LINE__, ps3_result(result));
281
282 return result;
283}
284
Geoff Levand7626e782007-06-16 08:01:06 +1000285static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800286 unsigned long *status)
287{
Geoff Levand7626e782007-06-16 08:01:06 +1000288 int result;
289 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800290 u64 tmp;
Geoff Levand7626e782007-06-16 08:01:06 +1000291
292 result = lv1_get_virtual_uart_param(dev->port_number,
Geoff Levand75c86e72007-02-13 17:37:28 -0800293 PARAM_INTERRUPT_STATUS, &tmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800294
295 if (result)
296 dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n",
297 __func__, __LINE__, ps3_result(result));
298
Geoff Levand7626e782007-06-16 08:01:06 +1000299 *status = tmp & priv->interrupt_mask;
Geoff Levand75c86e72007-02-13 17:37:28 -0800300
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000301 dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n",
Geoff Levand7626e782007-06-16 08:01:06 +1000302 __func__, __LINE__, priv->interrupt_mask, tmp, *status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800303
304 return result;
305}
306
Geoff Levand7626e782007-06-16 08:01:06 +1000307int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800308{
Geoff Levand7626e782007-06-16 08:01:06 +1000309 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
310
311 return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0
312 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800313 | INTERRUPT_MASK_TX);
314}
315
Geoff Levand7626e782007-06-16 08:01:06 +1000316int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800317{
Geoff Levand7626e782007-06-16 08:01:06 +1000318 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
319
320 return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0
321 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800322 | INTERRUPT_MASK_RX);
323}
324
Geoff Levand7626e782007-06-16 08:01:06 +1000325int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800326{
Geoff Levand7626e782007-06-16 08:01:06 +1000327 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
328
329 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0
330 : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800331 | INTERRUPT_MASK_DISCONNECT);
332}
333
Geoff Levand7626e782007-06-16 08:01:06 +1000334int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800335{
Geoff Levand7626e782007-06-16 08:01:06 +1000336 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
337
338 return (priv->interrupt_mask & INTERRUPT_MASK_TX)
339 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800340 & ~INTERRUPT_MASK_TX) : 0;
341}
342
Geoff Levand7626e782007-06-16 08:01:06 +1000343int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800344{
Geoff Levand7626e782007-06-16 08:01:06 +1000345 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
346
347 return (priv->interrupt_mask & INTERRUPT_MASK_RX)
348 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800349 & ~INTERRUPT_MASK_RX) : 0;
350}
351
Geoff Levand7626e782007-06-16 08:01:06 +1000352int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800353{
Geoff Levand7626e782007-06-16 08:01:06 +1000354 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
355
356 return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT)
357 ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800358 & ~INTERRUPT_MASK_DISCONNECT) : 0;
359}
360
361/**
362 * ps3_vuart_raw_write - Low level write helper.
Geoff Levand7626e782007-06-16 08:01:06 +1000363 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800364 *
365 * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write.
366 */
367
Geoff Levand7626e782007-06-16 08:01:06 +1000368static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000369 const void *buf, unsigned int bytes, u64 *bytes_written)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800370{
371 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000372 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800373
Geoff Levand7626e782007-06-16 08:01:06 +1000374 result = lv1_write_virtual_uart(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800375 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written);
376
377 if (result) {
378 dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: "
379 "%s\n", __func__, __LINE__, ps3_result(result));
380 return result;
381 }
382
Geoff Levand7626e782007-06-16 08:01:06 +1000383 priv->stats.bytes_written += *bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800384
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000385 dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand7626e782007-06-16 08:01:06 +1000386 *bytes_written, bytes, priv->stats.bytes_written);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800387
388 return result;
389}
390
391/**
392 * ps3_vuart_raw_read - Low level read helper.
Geoff Levand7626e782007-06-16 08:01:06 +1000393 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800394 *
395 * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read.
396 */
397
Geoff Levand7626e782007-06-16 08:01:06 +1000398static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000399 unsigned int bytes, u64 *bytes_read)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800400{
401 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000402 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800403
404 dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes);
405
Geoff Levand7626e782007-06-16 08:01:06 +1000406 result = lv1_read_virtual_uart(dev->port_number,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800407 ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read);
408
409 if (result) {
410 dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n",
411 __func__, __LINE__, ps3_result(result));
412 return result;
413 }
414
Geoff Levand7626e782007-06-16 08:01:06 +1000415 priv->stats.bytes_read += *bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800416
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000417 dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__,
Geoff Levand7626e782007-06-16 08:01:06 +1000418 *bytes_read, bytes, priv->stats.bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800419
420 return result;
421}
422
423/**
Geoff Levand75c86e72007-02-13 17:37:28 -0800424 * ps3_vuart_clear_rx_bytes - Discard bytes received.
Geoff Levand7626e782007-06-16 08:01:06 +1000425 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand75c86e72007-02-13 17:37:28 -0800426 * @bytes: Max byte count to discard, zero = all pending.
427 *
428 * Used to clear pending rx interrupt source. Will not block.
429 */
430
Geoff Levand7626e782007-06-16 08:01:06 +1000431void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev,
Geoff Levand75c86e72007-02-13 17:37:28 -0800432 unsigned int bytes)
433{
434 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000435 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -0800436 u64 bytes_waiting;
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100437 void *tmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800438
439 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting);
440
441 BUG_ON(result);
442
443 bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting;
444
445 dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes);
446
447 if (!bytes)
448 return;
449
450 /* Add some extra space for recently arrived data. */
451
452 bytes += 128;
453
454 tmp = kmalloc(bytes, GFP_KERNEL);
455
456 if (!tmp)
457 return;
458
459 ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting);
460
461 kfree(tmp);
462
463 /* Don't include these bytes in the stats. */
464
Geoff Levand7626e782007-06-16 08:01:06 +1000465 priv->stats.bytes_read -= bytes_waiting;
Geoff Levand75c86e72007-02-13 17:37:28 -0800466}
Geoff Levand7626e782007-06-16 08:01:06 +1000467EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes);
Geoff Levand75c86e72007-02-13 17:37:28 -0800468
469/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800470 * struct list_buffer - An element for a port device fifo buffer list.
471 */
472
473struct list_buffer {
474 struct list_head link;
475 const unsigned char *head;
476 const unsigned char *tail;
477 unsigned long dbg_number;
478 unsigned char data[];
479};
480
481/**
482 * ps3_vuart_write - the entry point for writing data to a port
Geoff Levand7626e782007-06-16 08:01:06 +1000483 * @dev: The struct ps3_system_bus_device instance.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800484 *
485 * If the port is idle on entry as much of the incoming data is written to
486 * the port as the port will accept. Otherwise a list buffer is created
487 * and any remaning incoming data is copied to that buffer. The buffer is
488 * then enqueued for transmision via the transmit interrupt.
489 */
490
Geoff Levand7626e782007-06-16 08:01:06 +1000491int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800492 unsigned int bytes)
493{
494 static unsigned long dbg_number;
495 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000496 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800497 unsigned long flags;
498 struct list_buffer *lb;
499
500 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
501 bytes, bytes);
502
Geoff Levand7626e782007-06-16 08:01:06 +1000503 spin_lock_irqsave(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800504
Geoff Levand7626e782007-06-16 08:01:06 +1000505 if (list_empty(&priv->tx_list.head)) {
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000506 u64 bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800507
508 result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written);
509
Geoff Levand7626e782007-06-16 08:01:06 +1000510 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800511
512 if (result) {
513 dev_dbg(&dev->core,
514 "%s:%d: ps3_vuart_raw_write failed\n",
515 __func__, __LINE__);
516 return result;
517 }
518
519 if (bytes_written == bytes) {
520 dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n",
521 __func__, __LINE__, bytes);
522 return 0;
523 }
524
525 bytes -= bytes_written;
526 buf += bytes_written;
527 } else
Geoff Levand7626e782007-06-16 08:01:06 +1000528 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800529
530 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL);
531
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100532 if (!lb)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800533 return -ENOMEM;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800534
535 memcpy(lb->data, buf, bytes);
536 lb->head = lb->data;
537 lb->tail = lb->data + bytes;
538 lb->dbg_number = ++dbg_number;
539
Geoff Levand7626e782007-06-16 08:01:06 +1000540 spin_lock_irqsave(&priv->tx_list.lock, flags);
541 list_add_tail(&lb->link, &priv->tx_list.head);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800542 ps3_vuart_enable_interrupt_tx(dev);
Geoff Levand7626e782007-06-16 08:01:06 +1000543 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800544
545 dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n",
546 __func__, __LINE__, lb->dbg_number, bytes);
547
548 return 0;
549}
Geoff Levand7626e782007-06-16 08:01:06 +1000550EXPORT_SYMBOL_GPL(ps3_vuart_write);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800551
552/**
Geoff Levand7626e782007-06-16 08:01:06 +1000553 * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list.
554 * @dev: The struct ps3_system_bus_device instance.
555 * @bytes_queued: Number of bytes queued to the buffer list.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800556 *
Geoff Levand7626e782007-06-16 08:01:06 +1000557 * Must be called with priv->rx_list.lock held.
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800558 */
559
Geoff Levand7626e782007-06-16 08:01:06 +1000560static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev,
561 u64 *bytes_queued)
562{
563 static unsigned long dbg_number;
564 int result;
565 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
566 struct list_buffer *lb;
567 u64 bytes;
568
569 *bytes_queued = 0;
570
571 result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes);
572 BUG_ON(result);
573
574 if (result)
575 return -EIO;
576
577 if (!bytes)
578 return 0;
579
580 /* Add some extra space for recently arrived data. */
581
582 bytes += 128;
583
584 lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC);
585
586 if (!lb)
587 return -ENOMEM;
588
589 ps3_vuart_raw_read(dev, lb->data, bytes, &bytes);
590
591 lb->head = lb->data;
592 lb->tail = lb->data + bytes;
593 lb->dbg_number = ++dbg_number;
594
595 list_add_tail(&lb->link, &priv->rx_list.head);
596 priv->rx_list.bytes_held += bytes;
597
Stephen Rothwella9dad6e2009-01-13 20:10:06 +0000598 dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n",
Geoff Levand7626e782007-06-16 08:01:06 +1000599 __func__, __LINE__, lb->dbg_number, bytes);
600
601 *bytes_queued = bytes;
602
603 return 0;
604}
605
606/**
607 * ps3_vuart_read - The entry point for reading data from a port.
608 *
609 * Queue data waiting at the port, and if enough bytes to satisfy the request
610 * are held in the buffer list those bytes are dequeued and copied to the
611 * caller's buffer. Emptied list buffers are retiered. If the request cannot
612 * be statified by bytes held in the list buffers -EAGAIN is returned.
613 */
614
615int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf,
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800616 unsigned int bytes)
617{
Geoff Levand7626e782007-06-16 08:01:06 +1000618 int result;
619 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800620 unsigned long flags;
621 struct list_buffer *lb, *n;
622 unsigned long bytes_read;
623
624 dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__,
625 bytes, bytes);
626
Geoff Levand7626e782007-06-16 08:01:06 +1000627 spin_lock_irqsave(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800628
Geoff Levand7626e782007-06-16 08:01:06 +1000629 /* Queue rx bytes here for polled reads. */
630
631 while (priv->rx_list.bytes_held < bytes) {
632 u64 tmp;
633
634 result = ps3_vuart_queue_rx_bytes(dev, &tmp);
635 if (result || !tmp) {
636 dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n",
637 __func__, __LINE__,
638 bytes - priv->rx_list.bytes_held);
639 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
640 return -EAGAIN;
641 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800642 }
643
Geoff Levand7626e782007-06-16 08:01:06 +1000644 list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800645 bytes_read = min((unsigned int)(lb->tail - lb->head), bytes);
646
647 memcpy(buf, lb->head, bytes_read);
648 buf += bytes_read;
649 bytes -= bytes_read;
Geoff Levand7626e782007-06-16 08:01:06 +1000650 priv->rx_list.bytes_held -= bytes_read;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800651
652 if (bytes_read < lb->tail - lb->head) {
653 lb->head += bytes_read;
Geoff Levand75c86e72007-02-13 17:37:28 -0800654 dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh "
655 "bytes\n", __func__, __LINE__, lb->dbg_number,
656 bytes_read);
Geoff Levand7626e782007-06-16 08:01:06 +1000657 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800658 return 0;
659 }
660
Geoff Levand75c86e72007-02-13 17:37:28 -0800661 dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh "
662 "bytes\n", __func__, __LINE__, lb->dbg_number,
663 bytes_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800664
665 list_del(&lb->link);
666 kfree(lb);
667 }
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800668
Geoff Levand7626e782007-06-16 08:01:06 +1000669 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800670 return 0;
671}
Geoff Levand7626e782007-06-16 08:01:06 +1000672EXPORT_SYMBOL_GPL(ps3_vuart_read);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800673
Geoff Levand7626e782007-06-16 08:01:06 +1000674/**
675 * ps3_vuart_work - Asynchronous read handler.
676 */
677
678static void ps3_vuart_work(struct work_struct *work)
Geoff Levandea1547d2007-02-06 14:23:47 -0800679{
Geoff Levand7626e782007-06-16 08:01:06 +1000680 struct ps3_system_bus_device *dev =
681 ps3_vuart_work_to_system_bus_dev(work);
682 struct ps3_vuart_port_driver *drv =
683 ps3_system_bus_dev_to_vuart_drv(dev);
684
685 BUG_ON(!drv);
686 drv->work(dev);
687}
688
689int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes)
690{
691 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levandea1547d2007-02-06 14:23:47 -0800692 unsigned long flags;
693
Geoff Levand7626e782007-06-16 08:01:06 +1000694 if (priv->rx_list.work.trigger) {
Geoff Levandea1547d2007-02-06 14:23:47 -0800695 dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n",
696 __func__, __LINE__);
697 return -EAGAIN;
698 }
699
700 BUG_ON(!bytes);
701
Geoff Levand7626e782007-06-16 08:01:06 +1000702 PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work);
Geoff Levandea1547d2007-02-06 14:23:47 -0800703
Geoff Levand7626e782007-06-16 08:01:06 +1000704 spin_lock_irqsave(&priv->rx_list.lock, flags);
705 if (priv->rx_list.bytes_held >= bytes) {
Geoff Levandea1547d2007-02-06 14:23:47 -0800706 dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n",
707 __func__, __LINE__, bytes);
Geoff Levand7626e782007-06-16 08:01:06 +1000708 schedule_work(&priv->rx_list.work.work);
709 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levandea1547d2007-02-06 14:23:47 -0800710 return 0;
711 }
712
Geoff Levand7626e782007-06-16 08:01:06 +1000713 priv->rx_list.work.trigger = bytes;
714 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
Geoff Levandea1547d2007-02-06 14:23:47 -0800715
716 dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__,
717 __LINE__, bytes, bytes);
718
719 return 0;
720}
Geoff Levand7626e782007-06-16 08:01:06 +1000721EXPORT_SYMBOL_GPL(ps3_vuart_read_async);
Geoff Levandea1547d2007-02-06 14:23:47 -0800722
Geoff Levand7626e782007-06-16 08:01:06 +1000723void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev)
Geoff Levandea1547d2007-02-06 14:23:47 -0800724{
Geoff Levand7626e782007-06-16 08:01:06 +1000725 to_port_priv(dev)->rx_list.work.trigger = 0;
Geoff Levandea1547d2007-02-06 14:23:47 -0800726}
Geoff Levand7626e782007-06-16 08:01:06 +1000727EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async);
Geoff Levandea1547d2007-02-06 14:23:47 -0800728
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800729/**
730 * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler
731 *
732 * Services the transmit interrupt for the port. Writes as much data from the
733 * buffer list as the port will accept. Retires any emptied list buffers and
734 * adjusts the final list buffer state for a partial write.
735 */
736
Geoff Levand7626e782007-06-16 08:01:06 +1000737static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800738{
739 int result = 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000740 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800741 unsigned long flags;
742 struct list_buffer *lb, *n;
743 unsigned long bytes_total = 0;
744
745 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
746
Geoff Levand7626e782007-06-16 08:01:06 +1000747 spin_lock_irqsave(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800748
Geoff Levand7626e782007-06-16 08:01:06 +1000749 list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800750
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000751 u64 bytes_written;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800752
753 result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head,
754 &bytes_written);
755
756 if (result) {
757 dev_dbg(&dev->core,
758 "%s:%d: ps3_vuart_raw_write failed\n",
759 __func__, __LINE__);
760 break;
761 }
762
763 bytes_total += bytes_written;
764
765 if (bytes_written < lb->tail - lb->head) {
766 lb->head += bytes_written;
767 dev_dbg(&dev->core,
Stephen Rothwellb17b3df2009-01-13 19:59:41 +0000768 "%s:%d cleared buf_%lu, %llxh bytes\n",
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800769 __func__, __LINE__, lb->dbg_number,
770 bytes_written);
771 goto port_full;
772 }
773
774 dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__,
775 lb->dbg_number);
776
777 list_del(&lb->link);
778 kfree(lb);
779 }
780
781 ps3_vuart_disable_interrupt_tx(dev);
782port_full:
Geoff Levand7626e782007-06-16 08:01:06 +1000783 spin_unlock_irqrestore(&priv->tx_list.lock, flags);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800784 dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n",
785 __func__, __LINE__, bytes_total);
786 return result;
787}
788
789/**
790 * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler
791 *
792 * Services the receive interrupt for the port. Creates a list buffer and
793 * copies all waiting port data to that buffer and enqueues the buffer in the
794 * buffer list. Buffer list data is dequeued via ps3_vuart_read.
795 */
796
Geoff Levand7626e782007-06-16 08:01:06 +1000797static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800798{
Geoff Levand7626e782007-06-16 08:01:06 +1000799 int result;
800 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800801 unsigned long flags;
Geoff Levand7626e782007-06-16 08:01:06 +1000802 u64 bytes;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800803
804 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
805
Geoff Levand7626e782007-06-16 08:01:06 +1000806 spin_lock_irqsave(&priv->rx_list.lock, flags);
807 result = ps3_vuart_queue_rx_bytes(dev, &bytes);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800808
Geoff Levand7626e782007-06-16 08:01:06 +1000809 if (result) {
810 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
811 return result;
Geoff Levandea1547d2007-02-06 14:23:47 -0800812 }
Geoff Levand7626e782007-06-16 08:01:06 +1000813
814 if (priv->rx_list.work.trigger && priv->rx_list.bytes_held
815 >= priv->rx_list.work.trigger) {
816 dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n",
817 __func__, __LINE__, priv->rx_list.work.trigger);
818 priv->rx_list.work.trigger = 0;
819 schedule_work(&priv->rx_list.work.work);
820 }
821
822 spin_unlock_irqrestore(&priv->rx_list.lock, flags);
823 return result;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800824}
825
826static int ps3_vuart_handle_interrupt_disconnect(
Geoff Levand7626e782007-06-16 08:01:06 +1000827 struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800828{
829 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
830 BUG_ON("no support");
831 return -1;
832}
833
834/**
835 * ps3_vuart_handle_port_interrupt - second stage interrupt handler
836 *
837 * Services any pending interrupt types for the port. Passes control to the
838 * third stage type specific interrupt handler. Returns control to the first
839 * stage handler after one iteration.
840 */
841
Geoff Levand7626e782007-06-16 08:01:06 +1000842static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800843{
844 int result;
Geoff Levand7626e782007-06-16 08:01:06 +1000845 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800846 unsigned long status;
847
Geoff Levand75c86e72007-02-13 17:37:28 -0800848 result = ps3_vuart_get_interrupt_status(dev, &status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800849
850 if (result)
851 return result;
852
853 dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__,
854 status);
855
856 if (status & INTERRUPT_MASK_DISCONNECT) {
Geoff Levand7626e782007-06-16 08:01:06 +1000857 priv->stats.disconnect_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800858 result = ps3_vuart_handle_interrupt_disconnect(dev);
859 if (result)
860 ps3_vuart_disable_interrupt_disconnect(dev);
861 }
862
863 if (status & INTERRUPT_MASK_TX) {
Geoff Levand7626e782007-06-16 08:01:06 +1000864 priv->stats.tx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800865 result = ps3_vuart_handle_interrupt_tx(dev);
866 if (result)
867 ps3_vuart_disable_interrupt_tx(dev);
868 }
869
870 if (status & INTERRUPT_MASK_RX) {
Geoff Levand7626e782007-06-16 08:01:06 +1000871 priv->stats.rx_interrupts++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800872 result = ps3_vuart_handle_interrupt_rx(dev);
873 if (result)
874 ps3_vuart_disable_interrupt_rx(dev);
875 }
876
877 return 0;
878}
879
Geoff Levand75c86e72007-02-13 17:37:28 -0800880struct vuart_bus_priv {
Geoff Levand7626e782007-06-16 08:01:06 +1000881 struct ports_bmp *bmp;
Geoff Levand75c86e72007-02-13 17:37:28 -0800882 unsigned int virq;
Geoff Levandc4e67522008-01-19 07:32:53 +1100883 struct mutex probe_mutex;
Geoff Levand75c86e72007-02-13 17:37:28 -0800884 int use_count;
Geoff Levand7626e782007-06-16 08:01:06 +1000885 struct ps3_system_bus_device *devices[PORT_COUNT];
Geoff Levand75c86e72007-02-13 17:37:28 -0800886} static vuart_bus_priv;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800887
888/**
889 * ps3_vuart_irq_handler - first stage interrupt handler
890 *
891 * Loops finding any interrupting port and its associated instance data.
892 * Passes control to the second stage port specific interrupt handler. Loops
893 * until all outstanding interrupts are serviced.
894 */
895
896static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private)
897{
Geoff Levand7626e782007-06-16 08:01:06 +1000898 struct vuart_bus_priv *bus_priv = _private;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800899
Geoff Levand7626e782007-06-16 08:01:06 +1000900 BUG_ON(!bus_priv);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800901
902 while (1) {
903 unsigned int port;
904
Geoff Levand7626e782007-06-16 08:01:06 +1000905 dump_ports_bmp(bus_priv->bmp);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800906
Geoff Levand7626e782007-06-16 08:01:06 +1000907 port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800908
909 if (port == BITS_PER_LONG)
910 break;
911
912 BUG_ON(port >= PORT_COUNT);
Geoff Levand75c86e72007-02-13 17:37:28 -0800913 BUG_ON(!bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800914
Geoff Levand75c86e72007-02-13 17:37:28 -0800915 ps3_vuart_handle_port_interrupt(bus_priv->devices[port]);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800916 }
917
918 return IRQ_HANDLED;
919}
920
Geoff Levand7626e782007-06-16 08:01:06 +1000921static int ps3_vuart_bus_interrupt_get(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800922{
923 int result;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800924
Geoff Levand7626e782007-06-16 08:01:06 +1000925 pr_debug(" -> %s:%d\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800926
Geoff Levand7626e782007-06-16 08:01:06 +1000927 vuart_bus_priv.use_count++;
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800928
Geoff Levand7626e782007-06-16 08:01:06 +1000929 BUG_ON(vuart_bus_priv.use_count > 2);
930
Geert Uytterhoevend0e5c2182008-01-19 07:32:19 +1100931 if (vuart_bus_priv.use_count != 1)
Geoff Levand7626e782007-06-16 08:01:06 +1000932 return 0;
Geoff Levand7626e782007-06-16 08:01:06 +1000933
934 BUG_ON(vuart_bus_priv.bmp);
935
936 vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL);
937
938 if (!vuart_bus_priv.bmp) {
939 pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__);
940 result = -ENOMEM;
941 goto fail_bmp_malloc;
942 }
943
944 result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp,
945 &vuart_bus_priv.virq);
946
947 if (result) {
948 pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n",
949 __func__, __LINE__, result);
950 result = -EPERM;
951 goto fail_alloc_irq;
952 }
953
954 result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler,
Yong Zhang6ea741a2011-10-21 23:56:53 +0000955 0, "vuart", &vuart_bus_priv);
Geoff Levand7626e782007-06-16 08:01:06 +1000956
957 if (result) {
958 pr_debug("%s:%d: request_irq failed (%d)\n",
959 __func__, __LINE__, result);
960 goto fail_request_irq;
961 }
962
963 pr_debug(" <- %s:%d: ok\n", __func__, __LINE__);
964 return result;
965
966fail_request_irq:
967 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
968 vuart_bus_priv.virq = NO_IRQ;
969fail_alloc_irq:
970 kfree(vuart_bus_priv.bmp);
971 vuart_bus_priv.bmp = NULL;
972fail_bmp_malloc:
973 vuart_bus_priv.use_count--;
974 pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -0800975 return result;
976}
977
Geoff Levand7626e782007-06-16 08:01:06 +1000978static int ps3_vuart_bus_interrupt_put(void)
979{
980 pr_debug(" -> %s:%d\n", __func__, __LINE__);
981
982 vuart_bus_priv.use_count--;
983
984 BUG_ON(vuart_bus_priv.use_count < 0);
985
986 if (vuart_bus_priv.use_count != 0)
987 return 0;
988
989 free_irq(vuart_bus_priv.virq, &vuart_bus_priv);
990
991 ps3_vuart_irq_destroy(vuart_bus_priv.virq);
992 vuart_bus_priv.virq = NO_IRQ;
993
994 kfree(vuart_bus_priv.bmp);
995 vuart_bus_priv.bmp = NULL;
996
997 pr_debug(" <- %s:%d\n", __func__, __LINE__);
998 return 0;
999}
1000
1001static int ps3_vuart_probe(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001002{
1003 int result;
Geoff Levand7626e782007-06-16 08:01:06 +10001004 struct ps3_vuart_port_driver *drv;
1005 struct ps3_vuart_port_priv *priv = NULL;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001006
1007 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1008
Geoff Levand7626e782007-06-16 08:01:06 +10001009 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1010
1011 dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__,
1012 drv->core.core.name);
1013
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001014 BUG_ON(!drv);
1015
Geoff Levand7626e782007-06-16 08:01:06 +10001016 if (dev->port_number >= PORT_COUNT) {
1017 BUG();
1018 return -EINVAL;
1019 }
1020
Geoff Levandc4e67522008-01-19 07:32:53 +11001021 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001022
Geoff Levand7626e782007-06-16 08:01:06 +10001023 result = ps3_vuart_bus_interrupt_get();
Geoff Levand75c86e72007-02-13 17:37:28 -08001024
Geoff Levand7626e782007-06-16 08:01:06 +10001025 if (result)
1026 goto fail_setup_interrupt;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001027
Geoff Levand7626e782007-06-16 08:01:06 +10001028 if (vuart_bus_priv.devices[dev->port_number]) {
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001029 dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__,
Geoff Levand7626e782007-06-16 08:01:06 +10001030 __LINE__, dev->port_number);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001031 result = -EBUSY;
Geoff Levand7626e782007-06-16 08:01:06 +10001032 goto fail_busy;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001033 }
1034
Geoff Levand7626e782007-06-16 08:01:06 +10001035 vuart_bus_priv.devices[dev->port_number] = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001036
Geoff Levand7626e782007-06-16 08:01:06 +10001037 /* Setup dev->driver_priv. */
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001038
Geoff Levand7626e782007-06-16 08:01:06 +10001039 dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv),
1040 GFP_KERNEL);
Geoff Levand75c86e72007-02-13 17:37:28 -08001041
Geoff Levand7626e782007-06-16 08:01:06 +10001042 if (!dev->driver_priv) {
Geoff Levand75c86e72007-02-13 17:37:28 -08001043 result = -ENOMEM;
Geoff Levand7626e782007-06-16 08:01:06 +10001044 goto fail_dev_malloc;
Geoff Levand75c86e72007-02-13 17:37:28 -08001045 }
1046
Geoff Levand7626e782007-06-16 08:01:06 +10001047 priv = to_port_priv(dev);
Geoff Levand75c86e72007-02-13 17:37:28 -08001048
Geoff Levand7626e782007-06-16 08:01:06 +10001049 INIT_LIST_HEAD(&priv->tx_list.head);
1050 spin_lock_init(&priv->tx_list.lock);
Geoff Levand75c86e72007-02-13 17:37:28 -08001051
Geoff Levand7626e782007-06-16 08:01:06 +10001052 INIT_LIST_HEAD(&priv->rx_list.head);
1053 spin_lock_init(&priv->rx_list.lock);
Geoff Levand75c86e72007-02-13 17:37:28 -08001054
Geoff Levand7626e782007-06-16 08:01:06 +10001055 INIT_WORK(&priv->rx_list.work.work, NULL);
1056 priv->rx_list.work.trigger = 0;
1057 priv->rx_list.work.dev = dev;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001058
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001059 /* clear stale pending interrupts */
Geoff Levand75c86e72007-02-13 17:37:28 -08001060
1061 ps3_vuart_clear_rx_bytes(dev, 0);
1062
1063 ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001064
1065 ps3_vuart_set_triggers(dev, 1, 1);
1066
1067 if (drv->probe)
1068 result = drv->probe(dev);
1069 else {
1070 result = 0;
1071 dev_info(&dev->core, "%s:%d: no probe method\n", __func__,
1072 __LINE__);
1073 }
1074
1075 if (result) {
1076 dev_dbg(&dev->core, "%s:%d: drv->probe failed\n",
1077 __func__, __LINE__);
1078 goto fail_probe;
1079 }
1080
Geoff Levandc4e67522008-01-19 07:32:53 +11001081 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001082
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001083 return result;
1084
1085fail_probe:
Geoff Levand75c86e72007-02-13 17:37:28 -08001086 ps3_vuart_set_interrupt_mask(dev, 0);
Geoff Levand7626e782007-06-16 08:01:06 +10001087 kfree(dev->driver_priv);
1088 dev->driver_priv = NULL;
1089fail_dev_malloc:
1090 vuart_bus_priv.devices[dev->port_number] = NULL;
1091fail_busy:
1092 ps3_vuart_bus_interrupt_put();
1093fail_setup_interrupt:
Geoff Levandc4e67522008-01-19 07:32:53 +11001094 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001095 dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001096 return result;
1097}
1098
Geoff Levand7626e782007-06-16 08:01:06 +10001099/**
1100 * ps3_vuart_cleanup - common cleanup helper.
1101 * @dev: The struct ps3_system_bus_device instance.
1102 *
1103 * Cleans interrupts and HV resources. Must be called with
1104 * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and
1105 * ps3_vuart_shutdown. After this call, polled reading will still work.
1106 */
1107
1108static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001109{
Geoff Levand7626e782007-06-16 08:01:06 +10001110 dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__);
1111
1112 ps3_vuart_cancel_async(dev);
1113 ps3_vuart_set_interrupt_mask(dev, 0);
1114 ps3_vuart_bus_interrupt_put();
1115 return 0;
1116}
1117
1118/**
1119 * ps3_vuart_remove - Completely clean the device instance.
1120 * @dev: The struct ps3_system_bus_device instance.
1121 *
1122 * Cleans all memory, interrupts and HV resources. After this call the
1123 * device can no longer be used.
1124 */
1125
1126static int ps3_vuart_remove(struct ps3_system_bus_device *dev)
1127{
1128 struct ps3_vuart_port_priv *priv = to_port_priv(dev);
1129 struct ps3_vuart_port_driver *drv;
1130
1131 BUG_ON(!dev);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001132
Geoff Levandc4e67522008-01-19 07:32:53 +11001133 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001134
Geoff Levand7626e782007-06-16 08:01:06 +10001135 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1136 dev->match_id);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001137
Geoff Levand7626e782007-06-16 08:01:06 +10001138 if (!dev->core.driver) {
1139 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1140 __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001141 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001142 return 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001143 }
Geoff Levand75c86e72007-02-13 17:37:28 -08001144
Geoff Levand7626e782007-06-16 08:01:06 +10001145 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1146
1147 BUG_ON(!drv);
1148
1149 if (drv->remove) {
1150 drv->remove(dev);
1151 } else {
1152 dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__,
1153 __LINE__);
1154 BUG();
1155 }
1156
1157 ps3_vuart_cleanup(dev);
1158
1159 vuart_bus_priv.devices[dev->port_number] = NULL;
1160 kfree(priv);
1161 priv = NULL;
1162
1163 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001164 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001165 return 0;
1166}
1167
1168/**
1169 * ps3_vuart_shutdown - Cleans interrupts and HV resources.
1170 * @dev: The struct ps3_system_bus_device instance.
1171 *
1172 * Cleans interrupts and HV resources. After this call the
1173 * device can still be used in polling mode. This behavior required
1174 * by sys-manager to be able to complete the device power operation
1175 * sequence.
1176 */
1177
1178static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev)
1179{
1180 struct ps3_vuart_port_driver *drv;
1181
1182 BUG_ON(!dev);
1183
Geoff Levandc4e67522008-01-19 07:32:53 +11001184 mutex_lock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001185
1186 dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__,
1187 dev->match_id);
1188
1189 if (!dev->core.driver) {
1190 dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__,
1191 __LINE__);
Geoff Levandc4e67522008-01-19 07:32:53 +11001192 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand7626e782007-06-16 08:01:06 +10001193 return 0;
1194 }
1195
1196 drv = ps3_system_bus_dev_to_vuart_drv(dev);
1197
1198 BUG_ON(!drv);
1199
1200 if (drv->shutdown)
1201 drv->shutdown(dev);
1202 else if (drv->remove) {
1203 dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n",
1204 __func__, __LINE__);
1205 drv->remove(dev);
1206 } else {
1207 dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__,
1208 __LINE__);
1209 BUG();
1210 }
1211
1212 ps3_vuart_cleanup(dev);
1213
1214 dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001215
Geoff Levandc4e67522008-01-19 07:32:53 +11001216 mutex_unlock(&vuart_bus_priv.probe_mutex);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001217 return 0;
1218}
1219
Geoff Levand7626e782007-06-16 08:01:06 +10001220static int __init ps3_vuart_bus_init(void)
Geert Uytterhoeven5b8e8ee2007-02-12 00:55:15 -08001221{
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001222 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand75c86e72007-02-13 17:37:28 -08001223
1224 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
Geert Uytterhoevenef596c62007-03-10 00:05:38 +01001225 return -ENODEV;
Geoff Levand75c86e72007-02-13 17:37:28 -08001226
Geoff Levandc4e67522008-01-19 07:32:53 +11001227 mutex_init(&vuart_bus_priv.probe_mutex);
Geoff Levand75c86e72007-02-13 17:37:28 -08001228
Geoff Levand7626e782007-06-16 08:01:06 +10001229 return 0;
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001230}
1231
Geoff Levand7626e782007-06-16 08:01:06 +10001232static void __exit ps3_vuart_bus_exit(void)
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001233{
1234 pr_debug("%s:%d:\n", __func__, __LINE__);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001235}
1236
Geoff Levand75c86e72007-02-13 17:37:28 -08001237core_initcall(ps3_vuart_bus_init);
1238module_exit(ps3_vuart_bus_exit);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001239
1240/**
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001241 * ps3_vuart_port_driver_register - Add a vuart port device driver.
1242 */
1243
1244int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv)
1245{
1246 int result;
1247
Geoff Levand7626e782007-06-16 08:01:06 +10001248 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1249
1250 BUG_ON(!drv->core.match_id);
1251 BUG_ON(!drv->core.core.name);
1252
1253 drv->core.probe = ps3_vuart_probe;
1254 drv->core.remove = ps3_vuart_remove;
1255 drv->core.shutdown = ps3_vuart_shutdown;
1256
1257 result = ps3_system_bus_driver_register(&drv->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001258 return result;
1259}
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001260EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register);
1261
1262/**
1263 * ps3_vuart_port_driver_unregister - Remove a vuart port device driver.
1264 */
1265
1266void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv)
1267{
Geoff Levand7626e782007-06-16 08:01:06 +10001268 pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name);
1269 ps3_system_bus_driver_unregister(&drv->core);
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001270}
Geoff Levand74e95d5d2006-12-08 18:27:47 -08001271EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister);